Monday, 15 August 2011

grep

grep filename

msRoot:~ ioannis$ ls -la | grep styl*
-rw-r--r--@ 1 ioannis staff 30295 16 Apr 03:29 style.css
msRoot:~ ioannis$



grep 'word' filename
grep 'string1 string2' filename
cat otherfile | grep 'something'
command | grep 'something'




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


You can search recursively i.e. read all files under each directory for a string "192.168.1.5"
$ grep -r "192.168.1.5" /etc/


Use grep to search words only

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" /path/to/file

Use grep to search 2 different words

use egrep as follows:
$ egrep -w 'word1|word2' /path/to/file

msRoot:~ ioannis$ ls -la | grep -c 'style'
1

Search for all contents "main" in .php
msRoot:~ ioannis$ grep -l 'main' *.php
single.php
msRoot:~ ioannis$


http://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/

No comments:

Post a Comment