0

I'm pretty new to AWK, and have question I hope someone can help me with: I have the a pattern that looks something like this:

620
621
622
623
624
624 66.75, 150.13
625
625 67.24, 153.31
626
627
628
628 68.14, 151.98

etc,etc

Using an awk script I'm trying to achieve the following result: I want to remove the duplicates from my first column of numbers (a counter) and concatenate my data (2nd & 3rd column). So I would like my data to look like this:

620
621
622
623
624 66.75, 150.13
625 67.24, 153.31
626
627
628 68.14, 151.98

So far I have been trying stuff like storing the first field in a variable and then test if the following number is equal or not. But I think I would have to do something with going back one step and change the previous printed variable if a duplicate field is found. How can I do this using AWK? Thanks a lot!

4

3 回答 3

2

此行将适用于您的示例:

 awk '{a[$1]=$0}END{for(x in a)print a[x]}' file

测试:

kent$  awk '{a[$1]=$0}END{for(x in a)print a[x]}' file
620
621
622
623
624 66.75, 150.13
625 67.24, 153.31
626
627
628 68.14, 151.98

但请注意for (x in a),尽管它适用于问题中的示例数据,但不能保证在您的文件中遵循相同的顺序。如果您希望结果按第一列排序。最简单的方法是将 awk 输出通过管道传输到 asort -n所以我们有:

 awk '{a[$1]=$0}END{for(x in a)print a[x]}' file|sort -n
于 2013-10-21T23:06:10.487 回答
1

对 Kent 代码的一个小调整(假设多行可以包含数据)

awk '{i=$1;sub(/[0-9]*/,"");a[i]=sprintf("%s %s", a[i], $0);}END{for(x in a)printf("%s %s\n", x, a[x]);}' td | sort

如果多行可以包含数据,则将它们连接在一起。

于 2013-10-21T23:24:41.640 回答
0

No need to store the entire file in memory:

awk 'l==$1{$1=""; printf ",%s", $0; next}
  {l=$1; printf("%s%s", NR==1?"":"\n", $0)}
  END{ print ""}' input-file
于 2013-10-22T13:46:31.957 回答