awk按列合并示例

需求:

将如下文本按第一列为主key,将相同的第二列值追加到同一行

cat input.log
ofB8Mt-5i_308H0LALOc-tsk1Aqg 2488
ofB8Mt-5i_308H0LALOc-tsk1Aqg 2620
ofB8Mt-j2usddmbu5_WrL3xitQ4o 2551
ofB8Mt-j2usddmbu5_WrL3xitQ4o 2582
ofB8Mt00DhYwMOpJvGm2dKDnZ3NM 3037
ofB8Mt00DhYwMOpJvGm2dKDnZ3NM 3080
ofB8Mt0UrL1Ux_PzudJiG0kH4uN4 1223
ofB8Mt0UrL1Ux_PzudJiG0kH4uN4 1248
ofB8Mt0WJTVB3am154f80kQ3bmv4 1169
ofB8Mt0WJTVB3am154f80kQ3bmv4 1207

执行命令:

cat input.log | awk '{a[$1]=a[$1]"|"$2}END{for( i in a){print i, a[i]"|"}}'
ofB8Mt00DhYwMOpJvGm2dKDnZ3NM |3037|3080|
ofB8Mt0UrL1Ux_PzudJiG0kH4uN4 |1223|1248|
ofB8Mt-5i_308H0LALOc-tsk1Aqg |2488|2620|
ofB8Mt-j2usddmbu5_WrL3xitQ4o |2551|2582|
ofB8Mt0WJTVB3am154f80kQ3bmv4 |1169|1207|

你可能感兴趣的:(awk)