awk to print unique lines based on column field:

2020-02-28 08:20发布

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

标签: awk
3条回答
冷血范
2楼-- · 2020-02-28 08:20

typo there in syntax.

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

查看更多
家丑人穷心不美
3楼-- · 2020-02-28 08:39

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
查看更多
虎瘦雄心在
4楼-- · 2020-02-28 08:43

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
查看更多
登录 后发表回答