从文件中删除字段中具有重复值的所有行,包括第一次出现的行 [英] Remove all lines from file with duplicate value in field, including the first occurrence

查看:105
本文介绍了从文件中删除字段中具有重复值的所有行,包括第一次出现的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除数据文件中所有包含第2列中的值的行,该行在其他行中的第2列中重复.

I would like to remove all the lines in my data file that contain a value in column 2 that is repeated in column 2 in other lines.

我已经按第2列中的值进行了排序,但是无法弄清楚如何仅对一个字段中的值使用uniq,因为这些值不一定具有相同的长度.

I've sorted by the value in column 2, but can't figure out how to use uniq for just the values in one field as the values are not necessarily of the same length.

或者,我可以使用像

awk -F"[,]" '!_[$2]++'

但是这保留了第2列中重复值第一次出现的行.

but this retains the line with the first incidence of the repeated value in col 2.

例如,如果我的数据是

a,b,c
c,b,a
d,e,f
h,i,j
j,b,h

我想删除在第二列中出现b的所有行(包括第一行). 像这样:

I would like to remove ALL lines (including the first) where b occurs in the second column. Like this:

d,e,f
h,i,j

感谢您的任何建议!

推荐答案

如果顺序不重要,则应该执行以下操作:

If the order is not important then the following should work:

awk -F, '
!seen[$2]++ {
    line[$2] = $0
}
END { 
    for(val in seen)
        if(seen[val]==1) 
          print line[val]
}' file

输出

h,i,j
d,e,f

这篇关于从文件中删除字段中具有重复值的所有行,包括第一次出现的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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