我需要到grep应该是一个多行字符串不包括一个字符串,但不包括其他人。这是我在一些HTML文件中搜索的内容:
<not-this>
<this> . . . </this>
</not-this>
换句话说,我想查找包含<this>
和</this>
在同一行中的文件,但不应<not-this>
在之前和/或之后的行中用html标记包围。这是我想做的一些速记逻辑:
grep 'this' && '/this' && !('not-this')
我已经看到以下问题的答案...
grep -Er -C 2 '.*this.*this.*' . | grep -Ev 'not-this'
...但是这只会删除包含“ not”部分的行,并显示其他行。我想要的是,如果在“ this”的一两行中没有找到“ not-this”,则根本不提取这些结果。
有没有办法做到这一点?
PS我正在使用Ubuntu和gnome-terminal。
听起来awk
脚本可以在这里更好地工作:
$ cat input.txt
<not-this>
<this>BAD! DO NOT PRINT!</this>
</not-this>
<yes-this>
<this>YES! PRINT ME!</this>
</yes-this>
$ cat not-this.awk
BEGIN {
notThis=0
}
/<not-this>/ {notThis=1}
/<\/not-this>/ {notThis=0}
/<this>.*<\/this>/ {if (notThis==0) print}
$ awk -f not-this.awk input.txt
<this>YES! PRINT ME!</this>
或者,如果您愿意,可以将此awk
脚本压缩到一长行中:
$ awk 'BEGIN {notThis=0} /<not-this>/ {notThis=1} /<\/not-this>/ {notThis=0} /<this>.*<\/this>/ {if (notThis==0) print}' input.txt
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句