开发者

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

开发者 https://www.devze.com 2023-01-29 17:12 出处:网络
I wish to print the non duplicated rows based on the 1st field using AWK. Could anyone please kindly help?

I wish to print the non duplicated rows based on the 1st field using 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.


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


For fixed number of characters in the first column and uniq implementation that supports -w option:

sort infile|uniq -uw1
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号