1

I have a command that outputs a bunch of data, but I only want two lines. Grep doesn't work because the lines I want are not next to one another. I can't seem to figure out sed...

Help please. :)

Example output:

Schedule:
   blabla:
   blabla:
   blabla:
   blabla:   
   blabla:
   blabla:
   Something Level:
   blabla:   
   blabla:
   blabla:

I want these lines:

Schedule:
   Something Level:

This output repeats through the different servers, and the output isn't the same. So I need to search for a pattern of two things over and over.

Bernard
  • 19
  • 2
  • Check out [this answer on Stack Overflow](http://stackoverflow.com/questions/152708/how-can-i-search-for-a-multiline-pattern-in-a-file-use-pcregrep) which might get you pointed in the right direction. If you have further questions and would like to ask them here please relate them to something in the context of professional system and network administration as defined in the scope of the [FAQ]. Thanks! – voretaq7 Apr 18 '13 at 19:28

2 Answers2

1

Just use grep's -v (invert match) and -E (Extended regexp) options:

$ grep -vE "Schedule:|Something Level:" filename
EEAA
  • 109,363
  • 18
  • 175
  • 245
  • When I pipe grep -E "Schedule:|Something Level:" it only shows the Schedule: lines. When I pipe grep -vE "Schedule:|Something Level:" it shows everything. – Bernard Apr 18 '13 at 18:14
  • 1
    not sure why you'd want to invert the match... for me grep -E "Schedule:|Something Level:" filename works perfectly. – senorsmile Apr 18 '13 at 18:20
  • Oops, I tried it again. I forgot one thing. grep -E 'Schedule:|Something Level:' This works. Thanks. – Bernard Apr 18 '13 at 18:42
1

Any of these will work, pick your favourite

grep 'Schedule:\|Something Level:'
grep -E 'Schedule:|Something Level:'

sed '/Schedule:\|Something Level:/!d'
sed -r '/Schedule:|Something Level:/!d'

sed -n '/Schedule:\|Something Level:/p'
sed -rn '/Schedule:|Something Level:/p'

awk '/Schedule:/ || /Something Level:/'
awk -F: '$1 == "Schedule" || $1 == "   Something Level"'

Have to be careful matching the whitespace on the last one.

glenn jackman
  • 4,630
  • 1
  • 17
  • 20