Welcome~~~


Another blog:
http://fun-st.blogspot.com/

It is easier to write an incorrect program than understand a correct one.

Thursday, March 31, 2011

Linux grep command examples

Linux grep command examples | grep command in Unix and Linux | grep examples | devdaily.com
http://www.devdaily.com/unix/edu/examples/grep.shtml

1) A typical grep example - searching for a text string in one file
grep 'joe' *.txt
grep 'fred' /etc/passwd
2) Linux grep command - searching for a string in multiple files

3) Case-insensitive file searching with the Unix grep command
-i grep -i score gettysburg-address.txt
4) Reversing the meaning of a grep search
-v grep -v score gettysburg-address.txt
5) Using grep in a Unix/Linux command pipeline

6) Using the Linux grep command to search for multiple patterns at one time (egrep)
egrep 'score|nation|liberty|equal' gettysburg-address.txt

7) Linux grep pattern matching and regular expressions (regex patterns)
grep '[0-9][0-9][0-9]' *
grep '[FG]oo' *
8) Display only filenames with a grep search
-l grep -l StartInterval *.plist
9) Showing matching line numbers with Linux grep
-n grep -n we gettysburg-address.txt
10) grep before/after - Showing lines before or after your grep pattern match
-B 5
-A 5 grep -A 5 "the living" gettysburg-address.txt
11) Power file searching with find and grep
find . -type f -exec grep -il 'foo' {} \;

"." means "look in the current directory"
"-type f" means "look in files only"
"-exec grep -il foo" means "search for the string 'foo' in a case-insensitive manner, and return the matching line and filename when a match is found
"{} \;" is some bizarre find syntax that you need to add to the end of your find command whenever you add the -exec option. (Sorry for my opinion there ... but you have to agree, that syntax is a little unusual.)

1 comment: