How to remove lines above and below an inverse grep match?

你离开我真会死。 提交于 2020-01-13 20:25:27

问题


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

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.

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?


回答1:


or with sed (gsed on osx):

sed ':a;N;$!ba;s/[^\n]*\nNo Problems Found\n//g'



回答2:


awk -v RS= -v ORS='\n\n' '/No Problems Found/' file

awk -v RS= -v ORS='\n\n' '!/Problem Found/' file


来源:https://stackoverflow.com/questions/20961661/how-to-remove-lines-above-and-below-an-inverse-grep-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!