Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep for lines above and below a certain pattern

Tags:

grep

I would like to search for a certain pattern (say Bar line) but also print lines above and below (i.e 1 line) the pattern or 2 lines above and below the pattern.

Foo  line
Bar line
Baz line

....

Foo1 line
Bar line
Baz1 line

....
like image 258
Rpj Avatar asked Oct 11 '13 08:10

Rpj


People also ask

How to grep a file but show several surrounding lines?

Possible duplicate of Grep a file, but show several surrounding lines? Use grep with the parameters -A and -B to indicate the number a of lines A fter and B efore you want to print around your pattern: An stands for n lines "after" the match. Bm stands for m lines "before" the match.

How to print around a pattern in grep?

Use grepwith the parameters -Aand -Bto indicate the number a of lines After and Before you want to print around your pattern: grep -A1 -B1 yourpattern file Anstands for nlines "after" the match. Bmstands for mlines "before" the match.

What is the difference between-1 and-B1 in grep?

grep's -A 1 option will give you one line after; -B 1 will give you one line before; and -C 1 combines both to give you one line both before and after, -1 does the same.

How to find the lines in a file that match multiple patterns?

Note, that you can both find the lines in a file that match multiple patterns in the exact order or in the any order. Use one of the following commands to find and print all the lines of a file, that match multiple patterns. Using grep command (exact order): $ grep -E 'PATTERN1.*PATTERN2' FILE. Using grep command (any order):


Video Answer


1 Answers

Use grep with the parameters -A and -B to indicate the number a of lines After and Before you want to print around your pattern:

grep -A1 -B1 yourpattern file
  • An stands for n lines "after" the match.
  • Bm stands for m lines "before" the match.

If both numbers are the same, just use -C:

grep -C1 yourpattern file

Test

$ cat file
Foo  line
Bar line
Baz line
hello
bye
hello
Foo1 line
Bar line
Baz1 line

Let's grep:

$ grep -A1 -B1 Bar file
Foo  line
Bar line
Baz line
--
Foo1 line
Bar line
Baz1 line

To get rid of the group separator, you can use --no-group-separator:

$ grep --no-group-separator -A1 -B1 Bar file
Foo  line
Bar line
Baz line
Foo1 line
Bar line
Baz1 line

From man grep:

   -A NUM, --after-context=NUM
          Print NUM  lines  of  trailing  context  after  matching  lines.
          Places   a  line  containing  a  group  separator  (--)  between
          contiguous groups of matches.  With the  -o  or  --only-matching
          option, this has no effect and a warning is given.

   -B NUM, --before-context=NUM
          Print  NUM  lines  of  leading  context  before  matching lines.
          Places  a  line  containing  a  group  separator  (--)   between
          contiguous  groups  of  matches.  With the -o or --only-matching
          option, this has no effect and a warning is given.

   -C NUM, -NUM, --context=NUM
          Print NUM lines of output context.  Places a line  containing  a
          group separator (--) between contiguous groups of matches.  With
          the -o or --only-matching option,  this  has  no  effect  and  a
          warning is given.
like image 65
fedorqui 'SO stop harming' Avatar answered Sep 20 '22 23:09

fedorqui 'SO stop harming'



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!