Wednesday, August 17, 2011

Advance usage of grep command

Guess output of following command

root# grep -2 vishesh /etc/passwd

Above command print not only line which contains string 'vishesh' but also print 2 previous lines and next 2 lines

Same result can also be obtained by following command
root# grep -C 2 vishesh /etc/passwd

But following command will print only next lines not previous
root#grep -A 2 vishesh /etc/passwd