awk将相同键值的字符串拼接一起输出

awk将相同键值的字符串拼接一起输出。每一行中第一列的数据是键,其余列为值。

文本1.txt中的内容是

abc 1 2 3 
abc a1 b1 c1
abc a2 b2 c2
abd a2 b2 c2
hello  hello_value1 hello_value2
hello  hello_value3 hello_value456
awk '{key=$1;$1="";value=$0;sum[key]=sum[key]""value} END{for(i in sum) print i,"=",sum[i]}' 1.txt 

注意,其中字符串拼接的操作是双引号””。

输出结果为

[root@localhost ~]# awk '{key=$1;$1="";value=$0;sum[key]=sum[key]""value} END{for(i in sum) print i,"=",sum[i]}' 2.txt 
hello =  hello_value1 hello_value2 hello_value3 hello_value456
abc =  1 2 3 a1 b1 c1 a2 b2 c2
abd =  a2 b2 c2
[root@localhost ~]#