Sign inSign up

marlabsdocker/lesson16

By marlabsdocker

•Updated over 8 years ago

Introduction to Linux. Lesson 16.Ten ways to use Grep command in Unix - examples.

0

0

marlabsdocker/lesson16 repository overview

⁠10 ways to use Grep command in Unix - examples

Following examples on grep command in UNIX are based on my experience and I use them on a daily basis in my work. Grep command is also part of any beginners UNIX command tutorialas it is an essential command to learn in order to work efficiently in any UNIX environment e..g Redhat Linux, Ubuntu, IBM AIX, Oracle Solaris or BSD. Anyway 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.

Example 1: How to ignore some words while doing search using grep in UNIX Finding relevant word and exclusion of irrelevant word. Most of the time I look for Exception and Errors in log files and sometimes I know certain Exception I can ignore so I use grep -v option to exclude those Exceptions

grep Exception logfile.txt | grep -v ERROR

This grep command example will search for word "Exception" in logfile.txt and print them but since we have piped out of first grep command to second grep command which will exclude all lines which match world "ERROR". To make this grep example more concrete let's see another example, here we have a file which contains three lines as shown below:

$ cat example.txt UNIX operating system UNIX and Linux operating system Linux operation system

Now we want to search all lines in file example.txt which contains word UNIX but same time doesn't contain world Linux.

$ grep UNIX example.txt UNIX operating system UNIX and Linux operating system

Now to exclude all lines which contain Linux we will apply another grep command in this output with option -v to exclude matching word as shown in below grep command:

$ grep UNIX example.txt | grep -v Linux UNIX operating system

Example 2: How to count occurrence of a word in a file using grep command If you want to count on a particular word in the log file you can use grep -c option to count the word. Below an example of command will print how many times word "Error" has appeared in logfile.txt.

$ grep -c "Error" logfile.txt

If we apply this grep command on our example file to find how many lines contains word e.g. UNIX has occurred in the file:

$ grep -c UNIX example.txt 2

Example 3: printing lines before and after of matching word using grep Sometimes we are not just interested in matching line but also on lines around matching lines particularly useful to see what happens before any Error or Exception. grep --contextoption 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 the 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.

Example 4: How to search pattern using egrep and regular expression 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

Example 5: How to do case insensitive searching using grep in Linux If you want to do case insensitive search then use -i option from grep command in UNIX. grep -i command will find an occurrence of both Error, error and ERROR and quite useful to display any sort of Error from the log file.

$ grep -i Error logfile

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

$ zgrep -i Error *.gz

Example 7: How to search the whole word in a file using grep command You can use grep -w command in UNIX to find the whole word instead of a just pattern, as shown in the following the example. This example will only print lines from logfile.txtwhich contains full word ERROR.

$ grep -w ERROR logfile.txt

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'.

Example 8: UNIX command to display files names which contain given word Another useful grep command line option is "grep -l" which display only the file names which match 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'.

Example 9: grep command option to display lines numbers If you want to see line number of matching lines you can use the option "grep -n" below command will show on which lines Error has appeared.

$ grep -n ERROR log file.

Example 10: How to do recursive search in a directory using grep in UNIX If you want to do a 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.

$ grep -R store *

This command will search for directory or file with the name stored in the current directory and it's all sub-directory.

Now I have two bonus examples of grep command in UNIX:

  1. grep command in UNIX can show matching pattern 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

You can also create alias grep='grep --color' in your bash_profile file to avoid typing --color every time.

  1. There are three versions of grep command in UNIX "grep, fgrep, and egrep". fgrep' stands for Fixed grep', egrep' Extended grep'

These examples of grep command in UNIX are something which I use on a daily basis; I have seen the most sophisticated use of grep with a 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 a 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.

Tag Summary

No tags have been pushed to this repository yet.