join all lines that have the same first column to the same line

拟墨画扇 提交于 2020-01-16 03:49:07

问题


IE:

File:

1234:abcd  
1234:930  
1234:999999  
194:keee  
194:284  
194:222222  

Result:

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!


回答1:


$ 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

How it works

  • -F:

    This tells awk to use a : as the field separator.

  • $1==last {printf ":%s",$2; next}

    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;}

    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.

Combining non-consecutive lines

Consider this test file:

$ cat testfile2
3:abcd
4:abcd
10:123
3:999
4:999
10:123

Apply this awk script:

$ 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

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.



来源:https://stackoverflow.com/questions/32623035/join-all-lines-that-have-the-same-first-column-to-the-same-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!