2

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?

Matt V.
  • 9,703
  • 10
  • 35
  • 56

2 Answers2

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

awk -v RS= -v ORS='\n\n' '!/Problem Found/' file
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
1

or with sed (gsed on osx):

sed ':a;N;$!ba;s/[^\n]*\nNo Problems Found\n//g'
Christian Fritz
  • 20,641
  • 3
  • 42
  • 71
  • Thanks @ChristianF, that appears to do what I needed! I think I follow part of it, but what is the `:a;N;$!ba;` part doing? – Matt V. Jan 07 '14 at 00:33
  • see here: http://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n. It basically allows you to match/replace across new lines – Christian Fritz Jan 07 '14 at 00:47
  • `"I think I follow part of it, but what is the :a;N;$!ba; part doing?"` - Priceless :-). – Ed Morton Jan 07 '14 at 01:41
  • @EdMorton, You will find the explanation here: http://www.grymoire.com/unix/sed.html – slayedbylucifer Jan 07 '14 at 05:38
  • @slayedbylucifer thanks but I wasn't looking for an explanation as IMHO it's crazy to use sed for something like this given the magical incantation of runes required. sed is an excellent tool for simple substitutions on a single line but once you start trying to parse multi-line text it's just pointless trying to do it with sed when it's so much simpler and clearer with awk. – Ed Morton Jan 07 '14 at 15:45