How to run grep and show x number of lines before and after the match

grep returns just the line where it matched the regex and often what I want to really see is a few (say 2) lines above and below the matched one.
Is there a simple way to achieve it?

EDIT:
OS: Ubuntu based Bodhi Linux.
As mentioned in comments, -C does not work in vanilla but its GNU grep in my case.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

From man grep:

Context Line Control

-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.

See how easy that was? man is your friend.

Method 2

Using (more portable) :

awk '
    {
        arr[NR]=$0
    }
    END{
        for (i=0;i<=NR;i++) {
            if (arr[i] ~ grep){
                for (j=i-count; j<=i+count; j++) {
                    print arr[j]
                }
            }
        }
    }
' grep=kdm count=4 /etc/passwd

I grep user kdm againts /etc/passwd with 4 lines before/after the match

In a script :

#!/bin/sh

awk '
    {
        arr[NR]=$0
    }
    END{
        for (i=0;i<=NR;i++) {
            if (arr[i] ~ grep){
                for (j=i-count; j<=i+count; j++) {
                    print arr[j]
                }
            }
        }
    }
' grep="$1" count="$2" "$3"

Usage :

./contextgrep <pattern> <count> <file>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x