简体   繁体   English

计数和操作awk或sed列变量

[英]Count and manipulate column variables awk or sed

I am trying to edit an array element as to convert a format of #1:#2,#3,... so that #1 would show file name, and #2-#n show their full file names, and have a count of #2-#n. 我正在尝试编辑数组元素以转换#1:#2,#3,...的格式,以便#1将显示文件名,而#2-#n显示其完整文件名,并具有#2-#n的数量

< EDIT > <编辑>
12:12,13 means: 12:12,13表示:
For ${names} entry "12", there exists entries in ${names} such that entries 12 and 13 are duplicates 对于$ {names}条目“ 12”,$ {names}中存在条目,因此条目12和13是重复项
< /EDIT > </编辑>

Contents of ${merge} $ {merge}的内容

key: 0 value: 12:12,13
key: 1 value: 18:18,19

Some contents of ${name} (just last part of ${path} $ {name}的某些内容(仅是$ {path}的最后一部分

key: 12 value: j.smith
key: 13 value: j.smith
key: 18 value: test
key: 19 value: test

Some contents of ${path} $ {path}的一些内容

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

My attempt: 我的尝试:

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


This is what I'm getting 这就是我得到的

File:  12,13  Number of Matches: 1
12,13 :  12,13
END

File:  18,19  Number of Matches: 1
18,19 :  18,19
END



What I'm trying to get: 我想要得到的是:

File: j.smith 档案:j.smith
Number of Matches: 2 比赛数量:2
./testDir/first/j.smith ./testDir/first/j.smith
./testDir/j.smith ./testDir/j.smith

File: test 文件:测试
Number of Matches: 2 比赛数量:2
./testDir/second/test ./testDir/second/test
./testDir/third/test ./testDir/third/test

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM