^ (Caret) | = | match expression at the start of a line, as in ^A. |
$ (Question) | = | match expression at the end of a line, as in A$. |
\ (Back Slash) | = | turn off the special meaning of the next character, as in \^. |
[ ] (Brackets) | = | match any one of the enclosed characters, as in [aeiou]. Use Hyphen "-" for a range, as in [0-9] . |
[^ ] | = | match any one character except those enclosed in [ ], as in [^0-9]. |
. (Period) | = | match a single character of any value, except end of line. |
* (Asterisk) | = | match zero or more of the preceding character or expression. |
\{x,y\} | = | match x to y occurrences of the preceding. |
\{x\} | = | match exactly x occurrences of the preceding. |
\{x,\} | = | match x or more occurrences of the preceding. |
grep smug files | {search files for lines with 'smug'} |
grep '^smug' files | {'smug' at the start of a line} |
grep 'smug$' files | {'smug' at the end of a line} |
grep '^smug$' files | {lines containing only 'smug'} |
grep '\^s' files | {lines starting with '^s', "\" escapes the ^} |
grep '[Ss]mug' files | {search for 'Smug' or 'smug'} |
grep 'B[oO][bB]' files | {search for BOB, Bob, BOb or BoB } |
grep '^$' files | {search for blank lines} |
grep '[0-9][0-9]' file | {search for pairs of numeric digits} |
grep '^From: ' /usr/mail/$USER | {list your mail} |
grep '[a-zA-Z]' | {any line with at least one letter} |
grep '[^a-zA-Z0-9] | {anything not a letter or number} |
grep '[0-9]\{3\}-[0-9]\{4\}' | {999-9999, like phone numbers} |
grep '^.$' | {lines with exactly one character} |
grep '"smug"' | {'smug' within double quotes} |
grep '"*smug"*' | {'smug', with or without quotes} |
grep '^\.' | {any line that starts with a Period "."} |
grep '^\.[a-z][a-z]' | {line start with "." and 2 lc letters} |
grep "unix" *.htm
search all .htm files in the current directory for any reference of unix and give results similar to the below example text.
Search /etc/passwd for boo user:
force grep to ignore word case i.e match boo, Boo, BOO and all other combination with -i option:
search recursively i.e. read all files under each directory for a string "192.168.1.5"
select only those lines containing matches that form whole words i.e. match only boo word:
the number of times that the pattern has been matched for each file using -c (count) option:
use -n option, which causes grep to precede each line of output with the number of the line in the text file from which it was obtained:
matches only those lines that do not contain the given word. For example print all line that do not contain the word bar:
print name of hard disk devices:
Display cpu model name:
the -l option to list file name whose contents mention main():
force grep to display output in colors:
prints all lines in the file that begin with the letter a, followed by any one character, then the letters ple.
grep for both "FS" and "HR" at the same time, but return lines that contain either entry.
egrep 'HR|FS' myfile
print all lines containing strings "abc" or "def" or both:
print all lines matching exactly "abc" or "def" :
$ grep boo /etc/passwd
force grep to ignore word case i.e match boo, Boo, BOO and all other combination with -i option:
$ grep -i "boo" /etc/passwd
search recursively i.e. read all files under each directory for a string "192.168.1.5"
$ grep -r "192.168.1.5" /etc/
select only those lines containing matches that form whole words i.e. match only boo word:
$ grep -w "boo" /path/to/file
the number of times that the pattern has been matched for each file using -c (count) option:
$ grep -c 'word' /path/to/file
use -n option, which causes grep to precede each line of output with the number of the line in the text file from which it was obtained:
$ grep -n 'word' /path/to/file
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 bar /path/to/file
print name of hard disk devices:
# dmesg | egrep '(s|h)d[a-z]'
Display cpu model name:
# cat /proc/cpuinfo | grep -i 'Model'
the -l option to list file name whose contents mention main():
$ grep -l 'main' *.c
force grep to display output in colors:
$ grep --color vivek /etc/passwd
prints all lines in the file that begin with the letter a, followed by any one character, then the letters ple.
grep ^a.ple fruitlist.txt
grep for both "FS" and "HR" at the same time, but return lines that contain either entry.
egrep 'HR|FS' myfile
print all lines containing strings "abc" or "def" or both:
grep -E 'abc|def'
print all lines matching exactly "abc" or "def" :
grep -E '^abc$|^def$'
No comments:
Post a Comment