Print the first line of uniq output
>cat testing.txt
aaa bbb
aaa ccc
xxx yyy
zzz ppp
uuu vvv
uuu ttt
I want to display the uniq lines based on the first field and output oly the first occurence of the line
aaa bbb
xxx yyy
zzz ppp
uuu vvv
When I do:
>uniq testing
I get:
aaa bbb
aaa ccc
xxx yyy
zzz ppp
uuu vvv
uuu ttt
Which is NOT what I want.开发者_JS百科
Another awk solution:
awk '!_[$1]++' infile
Perl:
perl -ane'
print unless $_{$F[0]}++
' infile
if you don't mind the order
$ awk '(!( $1 in arr) ){arr[$1]=$0}END{for(i in arr) print arr[i]}' file
Alternatively, you can use Ruby(1.9+)
$ ruby -ane 'BEGIN{h={}}; h[$F[0]]=$_ if not h.has_key?($F[0]) ; END{h.each{|x,y| puts "#{y}" }} ' file
aaa bbb
xxx yyy
zzz ppp
uuu vvv
Make sure you sort your input before passing it to uniq
cat testing.txt | sort | uniq -w3
uniq supports checking only N characters using the -w flag. If your file really looks like this, you can do uniq -w 3
.
精彩评论