1

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

3 Answers 3

0

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

Update:

to use as a function, write in a test.sh

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

then just

source test.sh

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
blah
blah
pattern  ->forget about the last 2 lines, start counting from here
blah 
blah

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:)

7
  • Awesome how can I make this a function like : jobcheck(){awk '/$1/ {for(i=0;i<10;i++){print;getline}}' $2 }
    – yatici
    May 8, 2013 at 15:51
  • I updated the answer for that :)
    – abasu
    May 8, 2013 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 May 8, 2013 at 16:44
  • yes, that's a good suggestion, updated the answer :)
    – abasu
    May 8, 2013 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, 2013 at 20:24
4

Using grep 2.10:

grep -A 10 "pattern" your_file

will print 10 lines after the match

From the grep man page:

-A num

--after-context=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
5
  • it says illegal option for me. I am on bash 3.0.19 I believe
    – yatici
    May 8, 2013 at 14:35
  • Your grep version is the important info. grep -V May 8, 2013 at 14:51
  • yea even that is an illegal option. I assume my stuff is really outdated?
    – yatici
    May 8, 2013 at 14:56
  • @yatici updated with an awk approach
    – Alex
    May 8, 2013 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, 2013 at 15:07
0

I'd use:

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

or

sed '1,/pattern/{/pattern/!d;}' | sed '12,$d'

You must log in to answer this question.