只有输出线如果特定的列值是唯一的 [英] Only output line if value in specific column is unique

查看:75
本文介绍了只有输出线如果特定的列值是唯一的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入:

line1 a gh
line2 a dd
line3 c dd
line4 a gg
line5 b ef

所需的输出:

line3 c dd
line5 b ef

这就是我想要的输出线只有在没有其他行包含列2 的值相同的情况下。我以为我可以用sort(例如排序-k2,2输入)和uniq的结合做到这一点,但现在看来,与uniq的我只能从左边跳到列(-F避免比较第一N场)。肯定有使用awk或有事做一些这方面的简单方法。

That is, I want to output line only in the case that no other line includes the same value in column 2. I thought I could do this with combination of sort (e.g. sort -k2,2 input) and uniq, but it appears that with uniq I can only skip columns from the left (-f avoid comparing the first N fields). Surely there's some straightforward way to do this with awk or something.

推荐答案

您可以结合AWK中,grep,sort和uniq一个快速的单行:

You can combine awk, grep, sort and uniq for a quick one-liner:

的grep -v^ [^] * $(AWK'{$打印2}input.txt的|排序| uniq的-d)input.txt的

编辑,避免了正则表达式,\\ +和\\反向引用:搜索结果的grep -v^ [^] * $(AWK'{$打印2}input.txt中|排序| uniq的-d | sed的'S / [^ + 0-9] / \\\\&安培/克')input.txt的

Edit, to avoid the regexes, \+ and \backreferences:

grep -v "^[^ ]* $(awk '{print $2}' input.txt | sort | uniq -d | sed 's/[^+0-9]/\\&/g') " input.txt

这篇关于只有输出线如果特定的列值是唯一的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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