Bash 计算和操作列变量awk或sed

Bash 计算和操作列变量awk或sed,bash,sed,awk,Bash,Sed,Awk,我试图将数组元素编辑为转换为#1:#2,#3,。。。因此,#1将显示文件名,#2-#n显示其完整文件名,并计数#2-#n。 12:12,13表示: 对于${names}条目“12,${names}中存在条目,因此条目12和13是重复的 ${merge}的内容 key: 0 value: 12:12,13 key: 1 value: 18:18,19 ${name}(只是${path}的最后一部分)的某些内容 key: 12 value: j.smith key: 13 value

我试图将数组元素编辑为转换为#1:#2,#3,。。。因此,#1将显示文件名,#2-#n显示其完整文件名,并计数#2-#n。


12:12,13表示:
对于${names}条目“12,${names}中存在条目,因此条目12和13是重复的

${merge}的内容

key: 0 value: 12:12,13
key: 1 value: 18:18,19
${name}(只是${path}的最后一部分)的某些内容

key: 12 value: j.smith
key: 13 value: j.smith
key: 18 value: test
key: 19 value: test
key: 12 value: ./testDir/first/j.smith
key: 13 value: ./testDir/j.smith
key: 18 value: ./testDir/second/test
key: 19 value: ./testDir/third/test
${path}的某些内容

key: 12 value: j.smith
key: 13 value: j.smith
key: 18 value: test
key: 19 value: test
key: 12 value: ./testDir/first/j.smith
key: 13 value: ./testDir/j.smith
key: 18 value: ./testDir/second/test
key: 19 value: ./testDir/third/test
我的尝试:

for ix in "${merge[@]}"
do
        # Remove "#:" from "#:#,#" string and pass #,# into awk
        echo ${ix} | sed s/[0-9]*:// | awk -v name="${name[*]}" '
        {
        FS=",";
        print "\File: ",$name," Number of Matches:",NF;
            for (ix=0; ix<NF; ix++)
            {
                print $ix,": ",$path[$ix];
            };
        print "END"; }'
done


我想要得到的是:

档案:j.smith
匹配数:2
/testDir/first/j.smith
/testDir/j.smith

文件:测试
匹配数:2
./testDir/second/test

./testDir/third/test

什么是
12:12,13
意思?为什么其中有两个
12
呢?对于数组${names}中12的值,这里是重复的文件:12,13(在重复文件列表中计数)。我在问题中添加了一个描述。谢谢!那么什么时候使用awk优于sed呢?另外,${ix$$$:*}和${ix##*:}我使用
awk
进行计算。
sed
用于
substitution
merge=([0]="12:12,13" [1]="18:18,19")
name=([12]="j.smith" [13]="j.smith" [18]="test" [19]="test")
path=([12]="./testDir/first/j.smith" [13]="./testDir/j.smith" [18]="./testDir/second/test" [19]="./testDir/third/test")

for ix in "${merge[@]}"
do
    a=${ix%%:*}
    b=(`echo ${ix##*:} | sed 's/,/ /g'`)
    n=${name[a]}
    echo
    echo "File: $n"
    echo "Number of Matches: ${#b[@]}"
    for i in ${b[@]}
    do
        echo ${path[$i]}
    done
done