Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

closed as off topic by mdpc, Dave M, voretaq7 Apr 18 '13 at 19:28

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Check out this answer on Stack Overflow 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 Answers 2

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

$ grep -vE "Schedule:|Something Level:" filename
share|improve this answer
    
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

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.

share|improve this answer
    
grep -E 'Schedule:|Something Level:' Works. Thanks –  Bernard Apr 18 '13 at 18:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.