简体   繁体   English

如何使用AWK根据字段打印不重复的行?

[英]How to print non duplicated rows based on a field with AWK?

I wish to print the non duplicated rows based on the 1st field using AWK. 我希望使用AWK根据第一个字段打印非重复行。 Could anyone please kindly help? 谁能帮忙吗?

Thanks 谢谢

    Input 

    1 28324 2077 2 1
    1 24682 2088 1 0
    1 25399 2074 1 0
    2 28925 1582 2 1
    3 30254 1450 1 0
    4 25552 1131 1 1
    4 31033 1134 1 0
    5 29230 1522 2 0


    Desired Output 
    2 28925 1582 2 1
    3 30254 1450 1 0
    5 29230 1522 2 0
awk '
(count[$1]++ < 1) { data[$1] = $0; }
END               { for (x in data) if (count[x] == 1) print data[x]; }
'

If the output should be sorted on the first column, pipe it through sort -nk1 . 如果输出应在第一列上sort -nk1 ,请通过sort -nk1其通过管道sort -nk1

If your data is sorted, you can use this which doesn't accumulate a potentially large array. 如果您的数据已排序,则可以使用此方法,它不会累积潜在的大数组。

awk '
    $1 != prev { if (count == 1) print line; count = 0 }
               { prev=$1; line = $0; ++count }
           END { if (count == 1) print }' inputfile

对于第一列和支持-w选项的uniq实现中固定数量的字符:

sort infile|uniq -uw1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM