I know how to use grep to output a line that matches a string. But what if I also want to output one line above every line containing a matching string, how do I do that?

link|improve this question

74% accept rate
feedback

1 Answer

up vote 2 down vote accepted

do the following

grep -B 1 text_to_search_for file.txt
link|improve this answer
2  
That should be a -B right? – OSX NINJA Aug 8 '10 at 0:06
Sorry, yes. -B for Before. -C is for before and after. Corrected answer, I had misread the question – bryan Aug 8 '10 at 0:18
feedback

Your Answer

 
or
required, but never shown

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