awk基于列值连接

时间:2017-06-06 18:43:37

标签: bash awk concatenation

我正在尝试根据第1列中的共享值连接第2列中的字符串。 我的文件是这样的:

2,John Doe
2,Roger Sterling
2,Blake Chaser
3,Rocky Horror
3,Jason Biggs
3,Bill Shakespeare

我需要这个:

2,John Doe, Roger Sterling, Blake Chaser
3,Rocky Horror, Jason Biggs, Bill Shakespeare

我尝试了很多awk命令,但我无法得到它。任何帮助表示赞赏

2 个答案:

答案 0 :(得分:1)

awk -F "," '{arr[$1] = arr[$1] FS $2} END {for (i in arr) print i arr[i]}' file

答案 1 :(得分:1)

尝试:以下内容将在Input_file中以$ 1的相同顺序提供输出。

awk -F, '!($1 in a){++i} {a[$1]=a[$1]?a[$1] OFS $NF:$1 OFS $NF;}{b[i]=a[$1]} END{;for(j=1;j<=i;j++){print b[j]}}' OFS=,   Input_file

EDIT1:此处也添加了非单线形式的解决方案,并逐行详细说明。我希望这会对你有所帮助。

awk -F, '!($1 in a){                            ####checking here if $1 is not present in array a. If not then go to following block to execute statements.
        ++i                                     ####increment value of variable named i with 1 each time cursor comes into this block.
                   }
                   {
        a[$1]=a[$1]?a[$1] OFS $NF:$1 OFS $NF;   ####putting array a value and concatenating its value if that is already present using ? and : operators.
                   }
                   {
        b[i]=a[$1]                              ####creating an array b whose index is value of variable i and whose value is array a with current lines $1.
                   }
        END        {;
        for(j=1;j<=i;j++){                      ####starting a for loop now whose value will go till the value of variable i from 1.
        print b[j]                              ####printing the value of array b whose index is variable j.
                         }
                   }
        ' OFS=,  file117                        ####mentioning the value of output field separator as , and mentioning the Input_file name too here.