Unix Tutorial : grep command and grep command examples

grep command Means – globally search regular expression

The grep utility searches text file.txt for a pattern and prints all lines that contain that pattern.

Syntax: grep [ -options ] limited-regular-expression [filename … ]

-c Print only a count of the lines that contain the pattern.
-i Ignore upper/lower case distinction during comparisons.
-l Print only the names of file.txt with matching lines,separated by NEWLINE characters. Does not repeat the names of file.txt when the pattern is found more than once.
-n Precede each line by its line number in the file (first line is 1).
-v Print all lines except those that contain the pattern.
-r It recursively search the pattern in all the file.txt in the current directory and all it’s subdirectory.
-w It searches the exact word

1: To find all uses of the word “top” (in any case) in the multiples file like x*, and write with line numbers:
grep -i -n top x*

2. grep CAT tmpfile – search ‘tmpfile’ for ‘CAT’ anywhere in a line

3. grep -i -w failure adpatch.log – search adpatch.log for word failure, any case

4. grep run[- ]time *.txt – find ‘run time’ or ‘run-time’ in all txt file.txt

5. who | grep appmmgr – pipe who to grep, look for appmmgr

Understanding Regular Expressions:

^ (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 \^. To look for a Caret “^” at the start of a line, the expression is ^\^.
[ ] (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. So b.b will match “bob”, “bib”, “b-b”, etc.
* (Asterisk) match zero or more of the preceding character or expression. An asterisk matches zero or more of what precedes it. Thus [A-Z]* matches any number of upper-case letters, including none, while [A-Z][A-Z]* matches one or more upper-case letters.

grep command examples 
grep kite file.txt {search file.txt for lines with ‘kite’}

grep ‘^kite’ file.txt {‘kite’ at the start of a line}

grep ‘kite$’ file.txt {‘kite’ at the end of a line}

grep ‘^kite$’ file.txt {lines containing only ‘kite’}

grep ‘\^s’ file.txt {lines starting with ‘^s’, “\” escapes the ^}

grep ‘[Kk]ite’ file.txt {search for ‘kite’ or ‘Kite’}

grep ‘T[oO][mM]’ file.txt {search for TOM, Tom, TOm or ToM }

grep ‘^$’ file.txt {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 ‘^.$’ {lines with exactly one character}

grep ‘”kite”‘ {‘kite’ within double quotes}

grep ‘”*kite”*’ {‘kite’, with or without quotes}

grep ‘^\.’ {any line that starts with a Period “.”}

grep ‘^\.[a-z][a-z]’ {line start with “.” and 2 lower case letters} letters.


If you want to search multiple words in the same grep command ,then use egrep command

egrep  ‘cat|bad|sat’  file.txt   : It search all the three words in the file


ps -ef|  egrep  -v  ‘cat|bad|sat’    : It discarded all the lines having any of these three word from the output of ps -ef