2

So I am trying to grep for a specific pattern and then print everything above and below that pattern up to a specific indicator. I don't know if this is possible with grep or if I should you some other tool like awk, sed, or generate some shell script. So if I have the following:

---------------
     .....
process: failed
     ......
----------------

and

----------------
     .....
process: frozen
     .....
----------------

I want to grep for 'process: frozen' and want everything between the dashed lines when 'process: frozen' is found. However the number of lines between the dashed lines may vary for different 'process: frozen' messages, so I can't count the number of lines above or below and use the -A and -B option of grep. Thank you in advance.

pdm
  • 145
  • 1
  • 2
  • 9

2 Answers2

6

I would use GNU awk and set the record separator to a string which contains 16 hypens:

awk '/process: (failed|frozen)/' RS='-{16}' input.file
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Doesn't work; `^` and `$` aren't taken to mean beginning and end of line but of input in this context. Also, regex `RS` is gawk-specific. – Wintermute Jul 07 '15 at 12:36
  • It looked like it should work at first glance, and the idea isn't bad in general. I might use `RS='\n-+\n'`, I think, to be on the safe side. Although this may lead to a first and last delimiter line making it into the output, which would have to be `sub`ed out. – Wintermute Jul 07 '15 at 12:40
  • I does work using `RS='-{16}'`. I like this solution most. The problem with `\n-+\n` is that on the first line the hyphens might not being prefixed by a `\n` and the last line might not being suffixed by a `\n`, but however this depends on the input format (it is not 100% clear atm). – hek2mgl Jul 07 '15 at 12:42
  • 1
    Depends on what can appear between the delimiter lines, I suppose. Anyway, it shows the general principle and is straightforward to amend in case of difficulties. Good enough for me. – Wintermute Jul 07 '15 at 12:44
  • @pdm You are welcome, but note that it will work with `GNU awk` only! Thanks for your help @Wintermute. – hek2mgl Jul 07 '15 at 12:46
  • 1
    Yeah the messages that are being generated wont have a '-' between the delimiter lines. So it should be good for all scenarios. But I think I will play it safe and go with RS='-{16}' – pdm Jul 07 '15 at 12:48
  • Is there a way to still print out the hyphens? – pdm Jul 07 '15 at 19:30
  • 1
    You can use the `ORS` (*output record separator*) variable. Like this: `awk '/process: (failed|frozen)/' RS='-{16}' ORS='-----------------' input.file` – hek2mgl Jul 07 '15 at 21:16
0

You can simply delete the line containing the process: frozen or process: failed message in sed.

sed "/process: frozen/d"
Alejandro
  • 3,040
  • 1
  • 21
  • 30