VNA
VNA

Reputation: 625

print unique lines based on field

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 ...

Upvotes: 10

Views: 24524

Answers (3)

bllidn3dd
bllidn3dd

Reputation: 1

typo there in syntax.

awk '{ if (!($1 in a)) a[$1] = $0; } END { for (i in a) print a[i]}'

Upvotes: 0

Edward Arthur
Edward Arthur

Reputation: 1

This should give you what you want:

awk -F, '{ if (!($1 in a)) a[$1] = $0; } END '{ for (i in a) print a[i]}' input.csv

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203635

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

Upvotes: 38

Related Questions