根据字段打印唯一的行 [英] print unique lines based on field

查看:51
本文介绍了根据字段打印唯一的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想基于first字段打印唯一行,保留该行的第一个匹配项,并删除重复的其他匹配项.

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

Input.csv

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

所需的输出:

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

推荐答案

您将对可见"的测试放在脚本的操作部分而不是条件部分中.更改为:

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

这篇关于根据字段打印唯一的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆