finding surrounding lines with grep

3

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?

tony_sid

Posted 2010-08-07T22:35:44.877

Reputation: 11 651

Answers

5

do the following

grep -B 1 text_to_search_for file.txt

bryan

Posted 2010-08-07T22:35:44.877

Reputation: 7 848

2That should be a -B right? – tony_sid – 2010-08-08T00:06:26.987

Sorry, yes. -B for Before. -C is for before and after. Corrected answer, I had misread the question – bryan – 2010-08-08T00:18:49.430