Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print unique lines based on field

Tags:

awk

Would like to print unique lines based on first field , keep the first occurrence of that line and remove duplicate other occurrences.

Input.csv

10,15-10-2014,abc
20,12-10-2014,bcd
10,09-10-2014,def
40,06-10-2014,ghi
10,15-10-2014,abc

Desired Output:

10,15-10-2014,abc
20,12-10-2014,bcd
40,06-10-2014,ghi

Have tried below command and in-complete

awk 'BEGIN { FS = OFS = "," }  { !seen[$1]++ } END { for ( i in seen) print $0}' Input.csv

Looking for your suggestions ...

like image 716
VNA Avatar asked Nov 11 '14 14:11

VNA


People also ask

How do you find unique lines in Unix?

The uniq command in UNIX is a command line utility for reporting or filtering repeated lines in a file. It can remove duplicates, show a count of occurrences, show only repeated lines, ignore certain characters and compare on specific fields.


1 Answers

You put your test for "seen" in the action part of the script instead of the condition part. Change it to:

awk -F, '!seen[$1]++' Input.csv

Yes, that's the whole script:

$ cat Input.csv
10,15-10-2014,abc
20,12-10-2014,bcd
10,09-10-2014,def
40,06-10-2014,ghi
10,15-10-2014,abc
$
$ awk -F, '!seen[$1]++' Input.csv
10,15-10-2014,abc
20,12-10-2014,bcd
40,06-10-2014,ghi
like image 92
Ed Morton Avatar answered Sep 21 '22 16:09

Ed Morton