How can I grep everything except the pattern and its next n lines?
我有一个文件:
names.dat:
1 2 | AAAA BBBB |
text.dat:
1 2 3 4 5 6 7 8 9 10 11 12 | AAAA CTGCTTCGTCA 12127567612 BBBB TCGACTACTAG 12331276318 CCCC TCATCATACAT 23612763812 DDDD GCTATCGCATC 23767263723 |
我想做的是(仅使用shell命令)从text.dat中排除那些也出现在name.dat中的行,以及(最重要的是)在公共行之后加三行。
所以基本上输出应该像这样:
1 2 3 4 5 6 | CCCC TCATCATACAT 23612763812 DDDD GCTATCGCATC 23767263723 |
您可以使用awk执行此操作:
1 | awk 'NR==FNR {a[$0]; next} $0 in a {i=0} ++i>3' names.dat text.dat |
进行测试:
1 2 3 4 5 6 7 | $ awk 'NR==FNR {a[$0]; next} $0 in a {i=0} ++i>3' names.dat text.dat CCCC TCATCATACAT 23612763812 DDDD GCTATCGCATC 23767263723 |
如果text.dat中的每一行都是唯一的:
1 | grep -Fxvf <(grep -f names.dat -A 2 text.dat | grep -v '^--' ) text.dat |
输出:
1 2 3 4 5 6 | CCCC TCATCATACAT 23612763812 DDDD GCTATCGCATC 23767263723 |
您可以使用
1 | grep -v"`grep -f name.dat -A 2 text.dat`" text.dat |
输出如下。
1 2 3 4 5 6 | CCCC TCATCATACAT 23612763812 DDDD GCTATCGCATC 23767263723 |
备注:
text.dat必须在" AAAA "和" BBBB "以及" BBBB "和" CCCC "之间有两行,因为" 2 "是魔术数字...
如果:
文件中没有选项卡,并且
这些模式中没有正则表达式元字符,然后:
1 2 3 4 5 | paste -sd'\\t\\t\ ' text.dat | grep -v -f <(mapfile -t a <names.dat;printf '^%s\\t\ '"${a[@]}") | tr \\\\t \\\ |
具有的优势是,如果需要的话,它可以真正地将三行的行进行比较。
正则表达式在反转下关闭。这意味着,如果您可以使用正则表达式匹配x,则可以使用正则表达式匹配除x之外的所有内容。
说
|BBBB\
)
|BBBB\
)([^\
]*\
){3}
]
传递参数