我正在尝试从应用程序日志文件中复制一些文本,但我真的不明白为什么它不起作用...首先,我提取文件中可能有我想要的内容的每个部分,然后我想排除那些我不想说的话。问题是我想删除第一个grep发现的整个块,而不仅仅是行。我正在尝试:
grep -A 5 "header of start parts" file.log | grep -v "piece of unwanted words" -A 2 -B 3
因此,我认为使用-A 2和-B 3会删除第二个grep中的整个块,但事实并非如此。它仍然给我带来不需要的单词的结果……这是我文件的模式:
Some text to ignore
Some Text to ignore
header of start parts
line to get together
line to get together
line to get together
line to get together
line to get together
Some text to ignore
header of start parts
line to get together
line with piece of unwanted words
line to get together
line to get together
line to get together
A lot of other logs and this patterns repeating
所以我的第一个grep给了我所有这种情况:
header of start parts
line to get together
line to get together
line to get together
line to get together
line to get together
和这个:
header of start parts
line to get together
line with piece of unwanted words
line to get together
line to get together
line to get together
第二个grep我想排除我发布的所有第二个块。感谢您的任何帮助。
由于grep
有益划上下文块(默认情况下,使用--
字符串),你可以在第一次的结果传递grep
到awk
以段落模式,并排除整个记录匹配第二个片段:
$ grep -A5 "header of start parts" file.log |
awk -vRS='\n--\n' '!/line with piece of unwanted words/'
header of start parts
line to get together
line to get together
line to get together
line to get together
line to get together
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句