1.ability to prase lines based on text and/or regularexpressions
3.searches case sentivitly by default
4.searches the keywaord anywhere anr prints the whole line it containts text
5.grep ia also called a line processor

Command Option/Flag Description Example
grep Empty pattern without any flag will display the content of file like “cat file.txt” grep ” file.txt
Match Control
-e Sets the matching pattern, can be used muitiple times to match muiltple patterns grep -e ‘pattern1’ -e ‘pattern2’ file.txt
-i Ignore case of the letters in pattern grep -i ‘paTTern’ file.txt
-v Invert match , Returns the lines that are not matching the pattern grep -v ‘pattern’ file.txt
-x Matches exactly. The pattern must match the entire line grep -x ‘pattern’ file.txt
-E Multiple Pattern match grep -E ‘pattern1|pattern2|pattern3’ files.txt
Output Control
-c Return the number of matches not content grep -c ‘pattern’ file.txt
-C Returns 2 lines above and below the matched pattern grep -C ‘pattern’ file.txt
-L Returns the filenames that do not match the pattern grep -L ‘pattern’ *.txt
-l Return the filenames that matches the pattern grep -l ‘pattern’ *.txt
-o Returns only the part of the line that matches the pattern,Oly matching pattern grep -o ‘pattern’ file.txt
-s Ignores the errors about the files that do not have access or unreadble grep -s ‘pattern’ *.txt
-R Return the files names which matches the pattern Recursively grep -R ‘pattern’ /root
Output Prefix Control
-n Print line numbers at prefix of the matched pattern grep -n ‘pattern’ file.txt
-H Print file names at prefix of matched pattern grep -H ‘pattern’ *.txt
-h Print only matches without file name grep -h ‘pattern’ *.txt
Output Line Control
-A NUM Prints the given number of lines After matching the pattern grep -A 3 ‘pattern’ file.txt
-B NUM Prints the given number of lines Before matching the pattern grep -B 3 ‘pattern’ file.txt
-C NUM Prints the given number of lines After & Before matching the pattern grep -C 3 ‘pattern’ file.txt
  • grep ‘linux’ file.txt
  • grep -i ‘linux’ file.txt
  • grep -n ‘linux’ file        (Prints lines with serial number)
  • grep ‘^linux’ file.txt    (prints the lines which satrts with txt linux)
  • grep -i ‘^linux’ file.txt
  • grep ‘linux$’ file.txt   (prints the lines which has linux at end of the line)
  • grep ‘[0-9]’ file.txt        (lines containing numbers anywhere on line)
  • grep -i ‘[a-z]’ file.txt     (lines containing characters)
  • grep ‘^[0-9]’ file.txt 
  • grep -c ‘linux’ file (gives the count of lines which has pattern linux)
  • grep -C ‘linux’ file  (Prints 2 lines , above and below matching line)
  • rpm -qa | grep -i grep (searches for grep pacakage in rpm database)
  • rpm -qa | grep ‘xorg’ |wc -l   (counts and displays the number of lines that has text “xorg”)
  • grep -v ‘linux’ file.txt     (inverted search :list all but not lines containing ‘linux’)
  • grep -e ‘^[<]’ file.txt        (gives all the lines which starts with (<))

1.Anchors are regular expressions(meta characters).they are used to match begning and end of the files
2.use single or double qoutes while using grep with regular expresions
3.Also execute grep using egrep when regular expresions are using

One of the more powerful constructs that egrep supports that grep does not is the pipe (|), which functions as an “or.” another way I could get the same result as above with a different query is:

egrep “food|boot”

Fgrep is the third member of the grep family. It stands for “fast grep” and for good reason. Fgrep is faster than other grep commands because it does not interpret regular expressions, it only searches for strings of literal characters. Fgrep is equivalent to grep -F.

fgrep “define$” file.txt

it will return the line “define$tuff” because it is not interpreting the dollar sign, only the entire string as literal characters.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s