bernie
bernie

Reputation: 823

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.

Upvotes: 0

Views: 1004

Answers (4)

Dimitre Radoulov
Dimitre Radoulov

Reputation: 28000

Another awk solution:

awk '!_[$1]++' infile

Perl:

perl -ane'
  print unless $_{$F[0]}++
  ' infile

Upvotes: 4

Angelom
Angelom

Reputation: 2531

Make sure you sort your input before passing it to uniq

cat testing.txt | sort | uniq -w3

Upvotes: 2

drysdam
drysdam

Reputation: 8637

uniq supports checking only N characters using the -w flag. If your file really looks like this, you can do uniq -w 3.

Upvotes: 1

kurumi
kurumi

Reputation: 25599

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

Upvotes: 2

Related Questions