"grep" one of the most frequently used UNIX command stands for "Global Regular Expression Print". This grep command tutorial is not about theory of UNIX grep but to practical use of grep in UNIX and here I am sharing my experience on use of grep command in Linux with an aim that this would serve as quick guide or tutorial for using grep in UNIX for new beginners and help them to understand the grep command better and its thoughtful usage in UNIX or Linux. Many people use grep just for finding words in a file and missed the real potential of grep by not using all its powerful command line options and its regular expression capability which could not only save a lot of time but also works as a great and powerful tool while analyzing large set of data or log files. Also find command in UNIX can be used in place of grep at many places.
Grep command Example in UNIX and Linux
Following examples on grep command in UNIX are based on my experience and I use them on daily basis in my work. These examples are by no means complete so please contribute your grep command tips or how you are using grep in Linux to make it more useful and allow all of us to benefit from each others experience and work efficiently in UNIX or Linux.So here we go
1) Finding relevant word and exclusion irrelevant word. Most of the time I look for Exception and Errors in log files and some time I know certain Exception I can ignore so I use grep -v option to exclude those Exceptions
grep Exception logfile.txt | grep -v ERROR
2) If you want to count of a particular word in log file you can use grep -c option to count the word. Below command will print how many times word "Error" has appeared in logfile.txt
grep -c "Error" logfile.txt
3) Sometime we are not just interested on matching line but also on lines around matching lines particularly useful to see what happens before any Error or Exception. grep --context option allows us to print lines around matching pattern. Below example of grep command in UNIX will print 6 lines around matching line of word "successful" in logfile.txt
grep --context=6 successful logfile.txt
Show additional six lines after matching very useful to see what is around and to print whole message if it splits around multiple lines. You can also use command line option "C" instead of "--context" for example
grep -C 2 'hello' *
Prints two lines of context around each matching line.
4) egrep stands for extended grep and it is more powerful than grep command in Unix and allows more regular exception like you can use "|" option to search for either Error or Exception by executing just one command.
egrep 'Error|Exception' logfile.txt
5) If you want to do case insensitive search than use -i option from grep command in UNIX. Grep -i will find occurrence of both Error, error and ERROR and quite useful to display any sort of Error from log file.
grep -i Error logfile
6) zgrep is another great version of grep command in Unix which is used to perform same operation as grep does but with .gz files. Many a times we gzip the old file to reduce size and later wants to look or find something on those file. zgrep is your man for those days. Below command will print all files which have "Error" on them.
zgrep -i Error *.gz
7) Use grep -w command in UNIX if you find whole word instead of just pattern.
grep -w ERROR logfile
Above grep command in UNIX searches only for instances of 'ERROR' that are entire words; it does not match `SysERROR'.
For more control, use `\<' and `\>' to match the start and end of words. For example:
grep 'ERROR>' *
Searches only for words ending in 'ERROR', so it matches the word `SysERROR'.
8) Another useful grep command line option is "grep -l" which display only the file names which matches the given pattern. Below command will only display file names which have ERROR?
grep -l ERROR *.log
grep -l 'main' *.java will list the names of all Java files in the current directory whose contents mention `main'.
9) If you want to see line number of matching lines you can use option "grep -n" below command will show on which lines Error has appeared.
grep -n ERROR log file.
10) If you want to do recursive search using grep command in Unix there are two options either use "-R" command line option or increase directory one by one as shown below.
Now I have two bonus examples of grep command in unix:
11) grep command in UNIX can show matching patter in color which is quite useful to highlight the matching section , to see matching pattern in color use below command.
grep Exception today.log --color
12) There are three version of grep command in UNIX `grep, fgrep, egrep'. `fgrep' stands for Fixed `grep', `egrep' Extended `grep'
These examples of grep command in UNIX are something which I use on daily basis; I have seen more sophisticated use of grep with regular expression. I will list some more examples of grep command in UNIX as I come across and find useful to share. As per my experience having good hold on grep and UNIX find command with knowledge of regular expression will be great for you day to day life if you need to look log files or config files or need to do production support on electronic trading systems or any other kind of system which is running on UNIX. This list of grep command in UNIX is by no means complete and I look forward from you guys to share how you are using grep command in UNIX.
Do you like Books?
If you love to read books here are few Java titles which is worth of money :

38 comments:
thank you mate...
thanks man
fantastic examples man. grep command rules the unix world.I just started with unix grep command and quite impress. your examples of grep command is worth bookmarking.I have also read your find command example tutorial and examples on both of them are quite useful.
great post! You gave a lot of very useful examples of the grep command.
Thanks Praveen , good to know that your like my examples of unix grep command.
nice post as well.. good job!
thanks, your examples of unix grep command is very handy. does these grep command examples will work in all Unix operating system e.g. Linux, solaris or AIX also ?
I was looking for grep command in unix with example to quickly start using grep in unix and I your grep tutorial helped me a lot. thanks
Fantastic article. Thank you Javin Paul.
what is difference between grep, zgrep, egrep and fgrep in unix ?
grep stands for global regular expression match and its common command available in most of Unix platform including Linux and Solaris.
egrep is extended grep which extends functionality of grep and support more regular expression than grep command , It is also much faster than original unix grep command.
fgrep is called full grep and it is used for exact match.
Why do you keep calling it as Unix grep command or grep command in Unix , why not simple grep or at most grep command ??
This tutorial has actually saved my hours of Ctrl F !!!!
grep Rocks !
you could have also included other options of unix grep command. your examples of unix grep command can also be more practical and organized. you could also list down operating system on which you have tested these grep examples e.g. Solaris, AIX, Linux or even Cygwin. nevertheless good tutorial.
Very useful :-)
Thanks a lot ... God bless you
@bhupesh and @ Anonymous, good to know that you like this unix grep command examples. thank you.
Some of the examples do not work in the standard Unix version of grep. It has no --context; that's a GNU-only option; so is -w.
And egrep is deprecated in favour of grep -E; fgrep is grep -F
You also omitted some other important options: -s, -q, -x
%F what use why use
Can these example of grep command in unix also work on other operating system based on Unix e.g. Redhat linux, Sun Solaris, IBM AIX or Ubuntu ? These are really useful commands and example but I want to be sure before I use them in other Unix OS.
unix grep -v and unix grep -c are my favorites. grep -v is used to exclude something from result e.g. grep -v Example will print all lines which doesn't contain "Example" word while grep -c is used to count number of lines matching to given pattern. grep -c Example will print number of lines which contains word "Example" in it.
I heard that grep command is also available in windows,does I can use these grep examples in windows as well ? Also can you please let me know what is recursive grep ?
I love your unix tutorial, and this unix grep tutorial is also very good. can you please post tutorial on unix sed command and unix cut command as well. thanks
One of my own favorites, a list of home folders >= 100MB, reversed sorted by size:
du -sm /home/* | sort -rn | grep -E "^[0-9]{3,}"
Can you please put egrep examples in linux and fgrep examples in unix , I am confused between egrep and fgrep and want to chose between egrep and fgrep. please guide with samples.
which grep version have you tested this example ? gnu grep ?
pretty useful grep examples, Please include fgrep examples and egrep examples as well.
any idea how to identify which options for grep are standard to use in shell scripts as there are so many variations in grep like gnu grep , unix grep, bsd grep ... if we use one option in grep n if it does not support in unix grep ..so tell any idea how to identify which options to use..?
hi whats the standard version to use for grep
Hi Anonymous, it depends which version of grep is available in your linux machine, normally most of Linux machine has gnu version of grep installed on them.
Hi Ankur, I presume only way is to test your script in those environment most of standard options like grep -c, grep -v are supported in all grep versions but to be sure I think only there man page or testing can do.
The Best Linux Ubuntu Commands, Complete examples best resource
http://thetechdata.blogspot.com/search/label/Linux
thanks a ton for this grep one liners in Linux. I have been using your grep command in Linux operating system from few days and it helped me a lot. I also liked your find command in Unix and Linux tutorial those are just amazing information. please do share some more command examples which we can use while doing support.
True power of grep command in linux comes when you search a file deep in directory. its easy to find a file using grep command in unix in current of specified directory but if you don't know where is your file than find command is better option over grep.
Hi, Thanks a lot!! You gave a lot of very useful examples of the grep command in unix.
I agree with Router, this site contains extremely useful example of grep command. no doubt that find and grep are two powerful search programs which is must know for any one working in Unix, Linux or Solaris but its always difficult to remember various options of grep or find and these examples helps to remember different options of unix grep command.
Guys any one knows about how to use grep command to find a file in a directory. I am struggling to find files inside directories i.e. program needs to check one after other directory to find files. please help
good post. context option of grep is very useful. thanks buddy
Post a Comment