将具有相同第一列的所有行连接到同一行 [英] join all lines that have the same first column to the same line
问题描述
IE:
文件:
1234:abcd
1234:930
1234:999999
194:keee
194:284
194:222222
结果:
1234:abcd:930:999999
194:kee:284:222222
据我所知,我已经筋疲力尽,无法想出办法.抱歉打扰你们!
I have exhausted my brain to the best of my knowledge and can't come up with a way. Sorry to bother you guys!
推荐答案
$ awk -F: '$1==last {printf ":%s",$2; next} NR>1 {print "";} {last=$1; printf "%s",$0;} END{print "";}' file
1234:abcd:930:999999
194:keee:284:222222
工作原理
-
-F:
这告诉
awk
使用:
作为字段分隔符.This tells
awk
to use a:
as the field separator.$1==last {printf ":%s",$2; next}
如果此行的第一个字段与最后一行的第一个字段相同,请打印一个冒号,然后打印第2个字段.然后,跳过其余命令,并从下一行开始.
If the first field of this line is the same as the first field of the last line, print a colon followed by field 2. Then, skip the rest of the commands and start over with the next line.
NR>1 {print "";}
如果到达此处,则表示此行的第一个字段具有新的未见值.如果这不是第一行,则通过打印换行符来结束最后一行.
If we get here, that means that this line has a new not-seen-before value of the first field. If this not the first line, we finish the last line by printing a newline character.
{last=$1; printf "%s",$0;}
使用字段1的新值更新变量
last
.然后,打印此行.Update the variable
last
with the new value of field 1. Then, print this line.END{print "";}
到达文件末尾后,打印最后一个换行符.
After we reach the end of the file, print one last newline character.
考虑此测试文件:
$ cat testfile2 3:abcd 4:abcd 10:123 3:999 4:999 10:123
应用以下awk脚本:
$ awk -F: '{a[$1]=a[$1]":"$2;} END{for (x in a) print x ":" substr(a[x],2);}' testfile2 3:abcd:999 4:abcd:999 10:123:123
在这种方法中,行不一定会以任何特定顺序出现.如果顺序很重要,则可能需要将此输出通过管道传输到
sort
.In this approach, the lines will not necessarily come out in any particular order. If order is important, you may want to pipe this output to
sort
.这篇关于将具有相同第一列的所有行连接到同一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!