Sunday, November 21, 2010

grep grep grep

Yeah, we all use google to "grep" for information. Well, recently I came to know about one option of grep which I never knew before.

grep -C[some number]

Check this out

Code:
root@bt> cat file
Apple
Bat
Cat
Dog
Elephant
Fan
Goat
Hat
Ink
Joker
King

root@bt> grep -n Fan file
6:Fan

root@bt> grep -n -C1 Fan file
5-Elephant
6:Fan
7-Goat

root@bt> grep -n -C2 Fan file
4-Dog
5-Elephant
6:Fan
7-Goat
8-Hat


As you can see,
-C1 will give you the one line before and after the match.
-C2 will give you two line before and after the match and so on.
-n option with grep will print the line number.
The -C[number] option will be handy if you want to see something before or after the line you are looking for.


Good Day!

No comments:

Post a Comment