简体   繁体   中英

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

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

sort infile|uniq -uw1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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