How to delete duplicated rows based in a column value?

后端 未结 4 2012
囚心锁ツ
囚心锁ツ 2021-01-05 06:03

Given the following table

 123456.451 entered-auto_attendant
 123456.451 duration:76 real:76
 139651.526 entered-auto_attendant
 139651.526 duration:62 real:         


        
4条回答
  •  天涯浪人
    2021-01-05 06:48

    uniq, by default, compares the entire line. Since your lines are not identical, they are not removed.

    You can use sort to conveniently sort by the first field and also delete duplicates of it:

    sort -t ' ' -k 1,1 -u file
    
    • -t ' ' fields are separated by spaces
    • -k 1,1: only look at the first field
    • -u: delete duplicates

    Additionally, you might have seen the awk '!a[$0]++' trick for deduplicating lines. You can make this dedupe on the first column only using awk '!a[$1]++'.

提交回复
热议问题