GREP command is one of the most frequently used UNIX command stands for "GlobalRegular Expression Print” .GREP command in Unix operating system e.g. Linux,Solaris, BSD, Ubuntu or IBM AIX is used to search files with matching patterns, byusing grep command in Unix you can search a file which contains a particular word orparticular pattern.Example 1Finding relevant word and exclusion of irrelevant word is the most feature of grepcommand. Most of the time I look for Exception and Errors in log files and some time Iknow certain Exception I can ignore so I use grep -v option to exclude those Exceptions.grep Exception logfile.txt | grep –v ERRORThis grep command example will search for word "Exception" in logfile.txt and printthem but since we have piped out of first grep command to second grep commandwhich will exclude all lines which match world "ERROR". To make this grep examplemore concrete lets see another example, here we have a file which contains three linesas shown below:$ cat example.txtUNIX operating systemUNIX and Linux operating systemLinux operating systemNow we want to search all lines in file example.txt which contains word UNIX but sametime doesnt contain world Linux.$ grep UNIX example.txtUNIX operating systemUNIX and Linux operating systemNow to exclude all lines which contains Linux we will apply another grep command inthis output with option -v to exclude matching word as shown in below grep command.$ grep UNIX example.txt | grep-v LinuxUNIX operating system
Example 2If you want to count of a particular word in log file you can use grep -c option to countthe word. Below example of command will print how many times word "Error" hasappeared in logfile.txt.grep -c "Error" logfile.txtIf we apply this grep command on our example file to find how many lines contains worde.g. UNIX has occurred in the file :$ grep -c UNIX example.txt2Example 3Sometime we are not just interested on matching line but also on lines around matchinglines 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 grepcommand in UNIX will print 6 lines around matching line of word "successful" inlogfile.txtgrep --context=6 successful logfile.txtShow additional six lines after matching very useful to see what is around and to printwhole message if it splits around multiple lines. You can also use command line option"C" instead of "--context" for examplegrep -C 2 helloPrints two lines of context around each matching line.Example 4egrep stands for extended grep and it is more powerful than grep command in Unix andallows more regular exception like you can use "|" option to search for either Error orException by executing just one command.egrep Error|Exception logfile.txt
Example 5If you want to do case insensitive search than use -i option from grep command inUNIX. Grep -i will find occurrence of both Error, error and ERROR and quite useful todisplay any sort of Error from log file.grep -i Error logfileExample 6Use grep -w command in UNIX if you find whole word instead of just pattern.grep -w ERROR logfileAbove grep command in UNIX searches only for instances of ERROR that are entirewords; 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.Example 7Another useful grep command line option is "grep -l" which display only the file nameswhich matches the given pattern. Below command will only display file names whichhave ERROR?grep -l ERROR *.loggrep -l main *.java will list the names of all Java files in the current directory whosecontents mention `main.
Example 8If you want to see line number of matching lines you can use option "grep -n" belowcommand will show on which lines Error has appeared.grep -n ERROR log file.