简体   繁体   中英

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.

< EDIT >
12:12,13 means:
For ${names} entry "12", there exists entries in ${names} such that entries 12 and 13 are duplicates
< /EDIT >

Contents of ${merge}

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

Some contents of ${name} (just last part of ${path}

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

Some contents of ${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
Number of Matches: 2
./testDir/first/j.smith
./testDir/j.smith

File: test
Number of Matches: 2
./testDir/second/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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