我正在一组文件中寻找模式#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/