Aug 8, 2012

Use grep to search file

Search /etc/passwd for boo user:
grep boo /etc/passwd
You can force grep to ignore word case i.e match boo, Boo, BOO and all other combination with -i option:
grep -i "boo" /etc/passwd
Use grep recursively

You can search recursively i.e. read all files under each directory for a string "boo":
grep -r "boo" /etc/
You can search for a string boo irrespective of word case and recursive:
grep -ir "boo" /etc/
When you search for boo, grep will match fooboo, boo123, etc. You can force grep to select only those lines containing matches that form whole words i.e. match only boo word:
grep -w "boo" /etc/
Grep invert match

You can use -v option to print inverts the match; that is, it matches only those lines that do not contain the given word. For example print all line that do not contain the word bar:
grep -v boo /etc/

No comments:

Post a Comment