linux - 使用列作为参数过滤文件中的重复项(grep linux)

标签 linux grep ack

我正在一组文件中寻找模式#type。作为输出,我应该返回包含该模式的行。这些行被组织为带有制表符分隔符的列:

<subject1> <#type> <object1>
<subject2> <#type> <object1>
<subject3> <#type> <object2>
<subject4> <#type> <object2>
<subject5> <#type> <object3>

为此,我使用命令 ack-grep :

$ack-grep "#type"

我还可以将 sed 用作:

sed -n -e "/#type/p;q"*.nt

问题是我应该避免的重复项是对象。作为输出我应该有:

 <subject1> <#type> <object1>
 <subject3> <#type> <object2>
 <subject5> <#type> <object3>

最佳答案

为什么不简单地使用旧的 grep?基本上应该是:

grep '#type' *.nt

要避免 objectN 部分出现重复,您可以将 uniq 与 --skip-fields 选项一起使用:

grep '#type' *.nt | sort -k3,3 | uniq --skip-fields 2

但是,在使用 uniq 之前需要对输出进行排序。

关于linux - 使用列作为参数过滤文件中的重复项(grep linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31586567/

相关文章:

从无符号字符缓冲区创建图像

c - x86_64 处理器的 sync(2) Linux 函数调用的定义在哪里

linux - 使用 sed/awk/grep 从 HTML 中解析包的版本?

regex - 带有 ack-grep 的多个模式?

linux - spin_lock 和 spin_unlock 是否会影响单 cpu 机器上 SMP 内核的性能?

Python:文件读取器 int 和 if 子句

bash - 如何在 bash 中过滤列值

heroku - Windows,heroku 配置 | grep MONGOLAB_URI

vim - 如何在 VIM 中包含 .go 以搜索 Ack?

regex - ack:通过正则表达式从搜索中排除特定目录