我有一个非常大的文本文件,其中包含类似于以下内容的数据:
he/PRP have/VBD obtain/VBN the/DT ##archbishopric/NN## against/IN the/DT monk/NNS of/IN the/DT
craft/NN ,/Fc he/PRP obtain/VBD the/DT ##archbishopric/NN## of/IN besancon/NP ;/Fx and/CC have/VBD it/PRP in/IN
======>match found: \#\#\sof\/IN
succeed/VBN to/TO the/DT ##archbishopric/NN## ./Fp
klutzy/NN little/JJ ##scene/NN## where/WRB 1/Z brave/JJ french/JJ man/NN refuse/VBZ to/TO sit/VB down/RP for/IN fear/NN of/IN be/VBG discover/VBN ./Fp
======>match found: \#\#\swhere\/WRB\s
我想使用grep匹配并删除所有包含“text”行的行,然后紧跟在找到 =====>匹配的新行字符后,如:
craft/NN ,/Fc he/PRP obtain/VBD the/DT ##archbishopric/NN## of/IN besancon/NP ;/Fx and/CC have/VBD it/PRP in/IN
======>match found: \#\#\sof\/IN
以换行符结尾。
因此,根据前面的例子,我想运行grep并获得以下输出
he/PRP have/VBD obtain/VBN the/DT ##archbishopric/NN## against/IN the/DT monk/NNS of/IN the/DT
succeed/VBN to/TO the/DT ##archbishopric/NN## ./Fp
我已经尝试过:grep -E -v '^.+\n======>match found:.+$' file.txt
建议here,将正则表达式.+*\n
附加到命令中以包含上一行,但它不起作用,是否有任何建议?
答案 0 :(得分:1)
这个sed
命令接近你想要的命令:
$ sed -n 'N;/\n======>match found:/d; P;D' textfile
he/PRP have/VBD obtain/VBN the/DT ##archbishopric/NN## against/IN the/DT monk/NNS of/IN the/DT
succeed/VBN to/TO the/DT ##archbishopric/NN## ./Fp
答案 1 :(得分:0)
由于传统的grep实现一次只考虑一行,所以多行grepping很复杂,因此在模式中添加\n
没有意义。
如果你有pcregrep,可以使用-M
标志进行多行匹配:
pcregrep -Mv '^.+\n======>match found:.+$'
输出:
he/PRP have/VBD obtain/VBN the/DT ##archbishopric/NN## against/IN the/DT monk/NNS of/IN the/DT
succeed/VBN to/TO the/DT ##archbishopric/NN## ./Fp