203

I want a way to search in a given text. For that, I use grep:

grep -i "my_regex"

That works. But given the data like this:

This is the test data
This is the error data as follows
. . . 
. . . .
. . . . . . 
. . . . . . . . .
Error data ends

Once I found the word error (using grep -i error data), I wish to find the 10 lines that follow the word error. So my output should be:

. . . 
. . . .
. . . . . . 
. . . . . . . . .
Error data ends

Are there any way to do it?

2

4 Answers 4

350

You can use the -B and -A to print lines before and after the match.

grep -i -B 10 'error' data

Will print the 10 lines before the match, including the matching line itself.

5
  • 1
    Thanks this is working. But when I tried to store this execution in the variable like this test=$(grep -i -B 10 'error' data), and print it using echo $test, I get the straight long lines as output.
    – sriram
    Sep 16, 2012 at 9:35
  • 1
    Thanks I figured out I need to do like this echo "$test" rather than echo $test
    – sriram
    Sep 16, 2012 at 9:37
  • 48
    -C 10 will print out 10 lines before AND after in one fell swoop! Jan 22, 2018 at 15:19
  • is there a way to do this using a specific before point? say the length i have to grab prior is variable?
    – Erudaki
    Jul 17, 2018 at 19:05
  • grep -i -m 1 -A 100 -B 150 'wordd' file.txt Sep 30, 2022 at 14:21
60

This prints 10 lines of trailing context after matching lines

grep -i "my_regex" -A 10

If you need to print 10 lines of leading context before matching lines,

grep -i "my_regex" -B 10

And if you need to print 10 lines of leading and trailing output context.

grep -i "my_regex" -C 10

Example

user@box:~$ cat out 
line 1
line 2
line 3
line 4
line 5 my_regex
line 6
line 7
line 8
line 9
user@box:~$

Normal grep

user@box:~$ grep my_regex out 
line 5 my_regex
user@box:~$ 

Grep exact matching lines and 2 lines after

user@box:~$ grep -A 2 my_regex out   
line 5 my_regex
line 6
line 7
user@box:~$ 

Grep exact matching lines and 2 lines before

user@box:~$ grep -B 2 my_regex out  
line 3
line 4
line 5 my_regex
user@box:~$ 

Grep exact matching lines and 2 lines before and after

user@box:~$ grep -C 2 my_regex out  
line 3
line 4
line 5 my_regex
line 6
line 7
user@box:~$ 

Reference: manpage grep

-A num
--after-context=num

    Print num lines of trailing context after matching lines.
-B num
--before-context=num

    Print num lines of leading context before matching lines.
-C num
-num
--context=num

    Print num lines of leading and trailing output context.
3
  • 9
    Nice, ive had to look this up a few times now, maybe I can remember it as -A(FTER) -B(EFORE) -C(ONTEXT)
    – Opentuned
    Aug 30, 2018 at 13:47
  • I am trying the same logic but getting below error - $ grep -A 20 'Exception in thread \"main\"' overnight.231122.log Nov 24, 2022 at 10:08
  • But I am getting this error - grep: illegal option -- A usage: grep [-E|-F] [-c|-l|-q] [-bhinsvwx] -e pattern_list... [-f pattern_file...] [file...] usage: grep [-E|-F] [-c|-l|-q] [-bhinsvwx] [-e pattern_list...] -f pattern_file... [file...] usage: grep [-E|-F] [-c|-l|-q] [-bhinsvwx] pattern [file...] [deuxvi1i:/pkg/vdc05/dirsrv05/overnight_log][dirsrv05]$ Nov 24, 2022 at 10:10
11

The way to do this is near the top of the man page

grep -i -A 10 'error data'
8

Try this:

grep -i -A 10 "my_regex"

-A 10 means, print ten lines after match to "my_regex"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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