Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to search a file for a pattern and if the it is found I want to see the line and 10 more lines of the result.

so far I have

grep -n pattern file | cut -d: f1 

now not sure how to use this out put to do the print with the logic like

sed -n result,(result+10)p file

probably going to have a few issued if it pattern is in multiple lines.

Any help is appreciated

share|improve this question

migrated from May 8 '13 at 22:35

This question came from our site for professional and enthusiast programmers.

up vote 0 down vote accepted

if grep -A is not working, try

awk '/pattern/ {for(i=0;i<number_of_lines;i++){print;getline}}' <filename>

else sed has another dirty solution

sed -n '/pattern/ {p;n;p;n;p;...}' <filename> here p-> print, n -> get to next line. So number of p is your number of lines to be printed


to use as a function, write in a

  awk "/$1/"' {for(i=0;i<10;i++){print;getline}}' $2

then just


to run,

jobcheck "pattern" "file"

Update: as per Jonathan Leffler's suggestion, if any of the next 10 lines contains the pattern, counting should start from that line so

pattern  ->start printing from here to next 10 lines
pattern  ->forget about the last 2 lines, start counting from here

So updated awk command will be like

awk '/pattern/{max_line=NR+2} {if(NR<=max_line) print}' <filename>

Similarly inside jobcheck will also be changed. Cheers:)

share|improve this answer
Awesome how can I make this a function like : jobcheck(){awk '/$1/ {for(i=0;i<10;i++){print;getline}}' $2 } – yatici May 8 '13 at 15:51
I updated the answer for that :) – abasu May 8 '13 at 16:15
Note that if the patterns appears in the next 10 lines, you should (probably) reset the 'print counter'. Or design it so that when you find the match, you set max_print = NR + 10; and then for each line, if (NR < max_print) print – Jonathan Leffler May 8 '13 at 16:44
yes, that's a good suggestion, updated the answer :) – abasu May 8 '13 at 17:03
hmm I put this function to my ~/.bashrc since it is short but I got an error saying syntax error: unexpected end of file. I didn't even put the function at the end of the file though. You know what might be the issue? – yatici May 8 '13 at 20:24

Using grep 2.10:

grep -A 10 "pattern" your_file

will print 10 lines after the match

From the grep man page:

-A num


Print num lines of trailing context after matching lines.

Using GNU awk 3.1.8:

awk '{if(a-->0) {print; next}} /pattern/{print; a=10}' your_file
share|improve this answer
it says illegal option for me. I am on bash 3.0.19 I believe – yatici May 8 '13 at 14:35
Your grep version is the important info. grep -V – Fred Thomsen May 8 '13 at 14:51
yea even that is an illegal option. I assume my stuff is really outdated? – yatici May 8 '13 at 14:56
@yatici updated with an awk approach – Alex May 8 '13 at 14:57
You might be using some stripped down version of grep. If you're on an RPM-based distribution you can probably do rpm -ql grep or on a DEB-based distribution, dpkg -l grep to get the version. – Wodin May 8 '13 at 15:07

I'd use:

sed -n '/pattern/{N;N;N;N;N;N;N;N;N;N;p;}'


sed '1,/pattern/{/pattern/!d;}' | sed '12,$d'
share|improve this answer

You must log in to answer this question.