Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

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.

share|improve this question

marked as duplicate by rahmu, warl0ck, Renan, Thor, Gilles Feb 27 '13 at 17:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
See Show lines matching a pattern and the 4 lines before each and How to display lines 2-4 after each grep result?. Certainly we have answers point to the -C option too, but can not find it… –  manatwork Feb 26 '13 at 18:59
    
Does your grep recognize -C2? –  Kevin Feb 26 '13 at 20:32
    
@Kevin yes it does. –  Anuvrat Parashar Feb 26 '13 at 22:27

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
    
That is for GNU grep, vanilla Unix grep hasn't got those. –  vonbrand Feb 26 '13 at 19:41
    
@vonbrand That I did not know. –  goldilocks Feb 26 '13 at 20:03

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> 
share|improve this answer

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