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. 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
精彩评论