Practical Example of grep command in unix

JavinPaul 9,361 views 16 slides Jun 17, 2011
Slide 1
Slide 1 of 16
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16

About This Presentation

Grep command is one of most useful command in unix. having mastery in Grep and find means your productivity will be very high in unix. these grep command tutorials contains some examples of grep command in unix. It teaches how to leverage power of grep command in unix or linux. This presentation con...


Slide Content

How to use Grep command How to use Grep command
in Unix or Linuxin Unix or Linux

Grep command in UnixGrep command in Unix
►This tutorial is about how to use grep command in This tutorial is about how to use grep command in
unix and here we will see some practical example unix and here we will see some practical example
of using grep in Unixof using grep in Unix
►"grep""grep" one of the most frequently used UNIX command one of the most frequently used UNIX command
stands for "Global Regular Expression Print". This grep stands for "Global Regular Expression Print". This grep
command tutorial is not about theory of command tutorial is not about theory of UNIX grepUNIX grep but to but to
practical use of grep in UNIX and here I am sharing my practical use of grep in UNIX and here I am sharing my
experience on use of experience on use of grep command in Linuxgrep command in Linux with an with an
aim that this would serve as quick guide or tutorial for aim that this would serve as quick guide or tutorial for
using grep in UNIX for new beginners and help them to using grep in UNIX for new beginners and help them to
understand the grep command better and its thoughtful understand the grep command better and its thoughtful
usage in UNIX or Linux. usage in UNIX or Linux.

How people use Grep in UnixHow people use Grep in Unix
►Many people use grep just for finding words Many people use grep just for finding words
in a file and missed the real potential of in a file and missed the real potential of
grep by not using all its powerful command grep by not using all its powerful command
line options and its regular expression line options and its regular expression
capability which could not only save a lot of capability which could not only save a lot of
time but also works as a great and powerful time but also works as a great and powerful
tool while analyzing large set of data or log tool while analyzing large set of data or log
files. Also files. Also find command in UNIXfind command in UNIX can be can be
used in place of grep at many places. used in place of grep at many places.

10 examples of grep command 10 examples of grep command
in UNIX and Linuxin UNIX and Linux
►Following Following examples on grep command in examples on grep command in
UNIXUNIX are based on my experience and I use are based on my experience and I use
them on daily basis in my work. These them on daily basis in my work. These
examples are by no means complete so examples are by no means complete so
please contribute your grep command tips please contribute your grep command tips
or how you are using grep in Linux to make or how you are using grep in Linux to make
it more useful and allow all of us to benefit it more useful and allow all of us to benefit
from each others experience and work from each others experience and work
efficiently in UNIX or Linux.efficiently in UNIX or Linux.
So here we go………….So here we go………….

Finding matching word and Finding matching word and
excluding unwanted excluding unwanted
►1) Finding relevant word and exclusion irrelevant 1) Finding relevant word and exclusion irrelevant
word. Most of the time I look for Exception and word. Most of the time I look for Exception and
Errors in log files and some time I know certain Errors in log files and some time I know certain
Exception I can ignore so I use grep -v option to Exception I can ignore so I use grep -v option to
exclude those Exceptionsexclude those Exceptions

►grep Exception logfile.txt | grep -v ERRORgrep Exception logfile.txt | grep -v ERROR

Counting number of matching wordCounting number of matching word
►2) If you want to count of a particular word 2) If you want to count of a particular word
in log file you can use grep -c option to in log file you can use grep -c option to
count the word. Below command will print count the word. Below command will print
how many times word "Error" has appeared how many times word "Error" has appeared
in logfile.txtin logfile.txt

►grep -c "Error" logfile.txtgrep -c "Error" logfile.txt

Displaying Context around matching Displaying Context around matching
wordword
►3) Sometime we are not just interested on matching line but also on 3) Sometime we are not just interested on matching line but also on
lines around matching lines particularly useful to see what happens lines around matching lines particularly useful to see what happens
before any Error or Exception. grep --context option allows us to print before any Error or Exception. grep --context option allows us to print
lines around matching pattern. Below example of grep command in lines around matching pattern. Below example of grep command in
UNIX will print 6 lines around matching line of word "successful" in UNIX will print 6 lines around matching line of word "successful" in
logfile.txtlogfile.txt

►grep --context=6 successful logfile.txtgrep --context=6 successful logfile.txt
►Show additional six lines after matching very useful to see what is Show additional six lines after matching very useful to see what is
around and to print whole message if it splits around multiple lines. around and to print whole message if it splits around multiple lines.
You can also use command line option "C" instead of "--context" for You can also use command line option "C" instead of "--context" for
example example
►grep -C 2 'hello' *grep -C 2 'hello' *
►Prints two lines of context around each matching line.Prints two lines of context around each matching line.

Using EGrep for sophisticated searchUsing EGrep for sophisticated search
►4) 4) egrepegrep stands for extended grep and it is stands for extended grep and it is
more powerful than grep command in Unix more powerful than grep command in Unix
and allows more regular exception like you and allows more regular exception like you
can use "|" option to search for either Error can use "|" option to search for either Error
or Exception by executing just one or Exception by executing just one
command. command.
►grep -i Error logfilegrep -i Error logfile

Using Zgrep to search in gzip fileUsing Zgrep to search in gzip file
►6) 6) zgrepzgrep is another great version of grep is another great version of grep
command in Unix which is used to perform same command in Unix which is used to perform same
operation as grep does but with .gz files. Many a operation as grep does but with .gz files. Many a
times we gzip the old file to reduce size and later times we gzip the old file to reduce size and later
wants to look or find something on those file. wants to look or find something on those file.
zgrep is your man for those days. Below command zgrep is your man for those days. Below command
will print all files which have "Error" on them.will print all files which have "Error" on them.

►zgrep -i Error *.gzzgrep -i Error *.gz

Finding whole word using grep in Finding whole word using grep in
Unix LinuxUnix Linux
►7) Use grep -w command in UNIX if you find whole word instead of 7) Use grep -w command in UNIX if you find whole word instead of
just pattern.just pattern.
►grep -w ERROR logfilegrep -w ERROR logfile

►Above grep command in UNIX searches only for instances of 'ERROR' Above grep command in UNIX searches only for instances of 'ERROR'
that are entire words; it does not match `SysERROR'. that are entire words; it does not match `SysERROR'.
►For more control, use `\<' and `\>' to match the start and end of For more control, use `\<' and `\>' to match the start and end of
words. For example:words. For example:

►grep 'ERROR>' * grep 'ERROR>' *

►Searches only for words ending in 'ERROR', so it matches the word Searches only for words ending in 'ERROR', so it matches the word
`SysERROR'.`SysERROR'.

Display only file names of matching Display only file names of matching
StringString
►8) Another useful grep command line option is 8) Another useful grep command line option is
"grep -l" which display only the file names which "grep -l" which display only the file names which
matches the given pattern. Below command will matches the given pattern. Below command will
only display file names which have ERROR?only display file names which have ERROR?

►grep -l ERROR *.loggrep -l ERROR *.log
►grep -l 'main' *.java will list the names of all Java grep -l 'main' *.java will list the names of all Java
files in the current directory whose contents files in the current directory whose contents
mention `main'.mention `main'.

Displaying line number of matches Displaying line number of matches
using grep in unixusing grep in unix
►9) If you want to see line number of 9) If you want to see line number of
matching lines you can use option "grep -n" matching lines you can use option "grep -n"
below command will show on which lines below command will show on which lines
Error has appeared.Error has appeared.
►grep -n ERROR log file.grep -n ERROR log file.

Recursive search using grep in UnixRecursive search using grep in Unix
►10) If you want to do recursive search using grep 10) If you want to do recursive search using grep
command in Unix there are two options either use command in Unix there are two options either use
"-R" command line option or increase directory "-R" command line option or increase directory
one by one as shown below.one by one as shown below.
►Grep –R HelloWorld *Grep –R HelloWorld *
►OrOr
►Grep HelloWorld */*Grep HelloWorld */*
►Grep HelloWorld */*/*Grep HelloWorld */*/*
►Everytime you put an aditional * it will search one Everytime you put an aditional * it will search one
level down.level down.

Now I have two bonus examples Now I have two bonus examples
of grep command in unix:of grep command in unix:
►11) 11) grep command in UNIXgrep command in UNIX can show matching patter can show matching patter
in color which is quite useful to highlight the matching in color which is quite useful to highlight the matching
section , to see matching pattern in color use below section , to see matching pattern in color use below
command.command.

►grep Exception today.log --colorgrep Exception today.log --color

►12) at last there are three version of grep command in 12) at last there are three version of grep command in
UNIX `grep, fgrep, egrep'. `fgrep' stands for Fixed `grep', UNIX `grep, fgrep, egrep'. `fgrep' stands for Fixed `grep',
`egrep' Extended `grep‘ use it based on your need.`egrep' Extended `grep‘ use it based on your need.

SummarySummary
►These These examples of grep command in UNIXexamples of grep command in UNIX are are
something which I use on daily basis; I have seen more something which I use on daily basis; I have seen more
sophisticated use of grep with regular expression. I will list sophisticated use of grep with regular expression. I will list
some more examples of grep command in UNIX as I come some more examples of grep command in UNIX as I come
across and find useful to share. As per my experience across and find useful to share. As per my experience
having good hold on grep and having good hold on grep and UNIX find commandUNIX find command with with
knowledge of regular expression will be great for you day knowledge of regular expression will be great for you day
to day life if you need to look log files or config files or to day life if you need to look log files or config files or
need to do production support on electronic trading need to do production support on electronic trading
systems or any other kind of system which is running on systems or any other kind of system which is running on
UNIX. This list of grep command in UNIX is by no means UNIX. This list of grep command in UNIX is by no means
complete and I look forward from you guys to share how complete and I look forward from you guys to share how
you are using grep command in UNIX. you are using grep command in UNIX.

AuthorAuthor
►Name: Javin PaulName: Javin Paul
►Website: Website:
http://javarevisited.blogspot.com/2011/06/10-examples-of-grep-command-in-unix-and.htmlhttp://javarevisited.blogspot.com/2011/06/10-examples-of-grep-command-in-unix-and.html
►Javin Paul is an expert in the area of Java, Unix Javin Paul is an expert in the area of Java, Unix
and TIBCO RV and and has been working with and TIBCO RV and and has been working with
these technology from past 7 years while working these technology from past 7 years while working
in various project in finance and trading domain.in various project in finance and trading domain.