如何删除逆向grep匹配上方和下方的行? [英] How to remove lines above and below an inverse grep match?
问题描述
我正在处理一些比我想要的更为冗长的输出,因此我试图使用grep将其缩减.输出看起来像这样……
I'm working with some output that is more verbose than I'd like, so I was trying to use grep to whittle it down. The output looks something like this…
path/to/file1:
No Problems Found
path/to/file3:
Problem Found
我想过滤掉所有与文件有关的输出,而不会出现问题.我可以通过grep -v "No Problems Found"
用管道输送输出来删除其中的一行.我以为我可以按照grep -B 1 -A 1 -v "No Problems Found"
的方式使用-B和-A,但事实证明,与-v结合使用时,它们不会反转.
I'd like to filter out all the output concerning files without problems. I'm able to remove one line of it by piping the output through grep -v "No Problems Found"
. I thought I'd then be able to use -B and -A along the lines of grep -B 1 -A 1 -v "No Problems Found"
but it turns out those don't invert when used in conjunction with -v.
导出文件后,我可以在Vim中快速修改输出,但我想直接在命令行上进行.有任何想法吗?对于Awk或Sed来说,这是更好的工作吗?
I can modify the output pretty quickly in Vim, after I've exported the file, but I'd like to do it directly on the command line, if I can. Any ideas? Is this a better job for Awk or Sed?
推荐答案
或使用sed(在osx上为ged):
or with sed (gsed on osx):
sed ':a;N;$!ba;s/[^\n]*\nNo Problems Found\n//g'
这篇关于如何删除逆向grep匹配上方和下方的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!