Thursday, July 27, 2023

10 examples of grep command in UNIX and Linux

Hello guys, if you want to learn the grep command in Linux and looking for some awesome, hands-on, easy to use tutorial then you have come to the right place. Earlier, I have shared many examples based tutorials to learn essential Linux commands like find, lsof, curl, sed, and chmod command and in this article, I am going to share 10 examples of grep command in Linux. The grep command is one of the most frequently used UNIX command stands for "Global Regular Expression Print" along with find, chmod, and tar command in UNIX. grep command in Unix operating systems like Linux, Solaris, BSD, Ubuntu, or IBM AIX is used to search files for matching patterns.

By using the grep command in Unix you can search a file that contains a particular word or particular pattern. UNIX grep command also provides several useful command-line options that can be used to enhance the functionality of the grep command.

Similarly, by using grep -v you can list down all files which don't contain a word I mean excluding files which match a pattern, grep -c will print the count of matching pattern in a file, etc. One of the popular examples of the grep command is to find empty files and directories in Unix.

This grep command tutorial is not about the theory of UNIX grep but the practical use of grep command in UNIX and here I am sharing my experience on the use of grep command in Linux with an aim that this would serve as a 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 a large set of data or log files.

By the way, if you are new to Linux then I also suggest you go through a comprehensive Linux course to learn some basics commands and fundamentals like Linux file system, permissions, and other basic things.




10 ways to use Grep command in Unix - examples

The following examples of 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 beginner's UNIX command tutorial as it is an essential command to learn in order to work efficiently in any UNIX environment like Redhat Linux, Ubuntu, IBM AIX, Oracle Solaris, or BSD.

Anyway, these examples are by no means complete so please contribute you're own 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 other's experience and work efficiently in UNIX or Linux.


Example 1: How to ignore some words while doing a search using grep in UNIX

Finding relevant words and exclusion of irrelevant words. 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 the grep -v option to exclude those Exceptions

grep Exception logfile.txt | grep -v ERROR

This grep command example will search for the word "Exception" in logfile.txt and print them but since we have piped out of the first grep command to the second grep command which will exclude all lines which match the word "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 the word UNIX but at the same time doesn't contain the word 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
You can see how useful the grep -v command option can be.

grep command examples in Linux




Example 2: How to count the occurrence of a word in a file using the grep command

If you want to count on a particular word in the log file you can use the grep -c option to count the word. Below an example of a command will print how many times the 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 contain 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 lines 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 an example of grep command in UNIX will print 6 lines around the matching line of word "successful" in logfile.txt

$ grep --context=6 successful logfile.txt

Show an 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 expression 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 a case insensitive search then use -i option from the grep command in UNIX. grep -i command will find any 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 the 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 want to look or find something on those files. zgrep is your man for those days. 

The below grep command will print all files which have "Error" on them.

$ zgrep -i Error *.gz



Example 7: How to do a recursive search in a directory using grep in UNIX

If you want to do a recursive search using the 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 a directory or file with the name stored in the current directory and it's all sub-directory. If you want to learn more you can also see this recursive grep example from my other article. 



Example 8: UNIX command to display files names which contain given word 

Another useful grep command-line option is "grep -l" which displays only the file names which match the given pattern. Below command will only display file names that 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'.

Also, find command in UNIX can be used in place of grep in many places. 



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 search the whole word in a file using the grep command

You can use the grep -w command in UNIX to find the whole word instead of a just pattern, as shown in the following example. This example will only print lines from logfile.txt which 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'.




Bonus GREP  Examples

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

11) grep command in UNIX can show a 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.

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


And, here is a nice summary of all the grep commands you have learned so far. You can take the print out of this slide to keep these grep commands handy.

10 ways to use GREP command in UNIX



These examples of grep command in UNIX are something that 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 it 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 your day to day life if you need to look at 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 to you guys sharing how you are using the grep command in UNIX.



40 comments:

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

    ReplyDelete
  2. 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 ?

    ReplyDelete
  3. 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

    ReplyDelete
  4. what is difference between grep, zgrep, egrep and fgrep in unix ?

    ReplyDelete
  5. 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.

    ReplyDelete
  6. Why do you keep calling it as Unix grep command or grep command in Unix , why not simple grep or at most grep command ??

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

    ReplyDelete
  8. @bhupesh and @ Anonymous, good to know that you like this unix grep command examples. thank you.

    ReplyDelete
  9. 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

    ReplyDelete
  10. %F what use why use

    ReplyDelete
  11. 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.

    ReplyDelete
  12. 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.

    ReplyDelete
  13. 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 ?

    ReplyDelete
  14. 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

    ReplyDelete
  15. 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,}"

    ReplyDelete
  16. 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.

    ReplyDelete
  17. which grep version have you tested this example ? gnu grep ?

    ReplyDelete
  18. pretty useful grep examples, Please include fgrep examples and egrep examples as well.

    ReplyDelete
  19. 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..?

    ReplyDelete
  20. hi whats the standard version to use for grep

    ReplyDelete
  21. 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.

    ReplyDelete
  22. 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.

    ReplyDelete
  23. The Best Linux Ubuntu Commands, Complete examples best resource
    http://thetechdata.blogspot.com/search/label/Linux

    ReplyDelete
  24. 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.

    ReplyDelete
  25. 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.

    ReplyDelete
  26. 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.

    ReplyDelete
  27. 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

    ReplyDelete
  28. good post. context option of grep is very useful. thanks buddy

    ReplyDelete
  29. Hi Javin, thanks for this, much appreciated! I would like to share two more grep goodies I am frequently using:

    - grep -A x adds x lines to each matching line. Useful for finding information for which the match is only the headline.

    - In order to prevent grep from finding itself f.e. in ps -ef output or in the history, use square brackets around the first character. This regular expression tricks grep into searching for the wanted string while the string itself does not occur in the command. Example:

    ps -ef | grep [j]ava

    shows all running java processes while it will not show 'grep java' in the result. Much less typing than

    ps -ef | grep java | grep -v grep

    and one process less in the pipe.

    Kind regards,
    Ralph Kirchner

    ReplyDelete
  30. What is the grep command for following find command in Unix :

    find . -type d -perm -o=w

    which will find all files which are open and Have write permission on it.

    ReplyDelete
  31. Can you please suggest grep command examples for following scenario:

    How to grep for String with spaces something like
    grep ' ABC' file

    I think this will work because quotes will include the spaces as well while searching.

    How to grep for String with double quotes inside like

    grep '"ABC"' file

    this will also work because single quote treats double quotes as literal, please check if it doesn't work and why.

    How to grep for String with single quotes in it?
    for example if I want to search for word don't which has single quote on it, will following grep command work :

    grep "don't" file

    How to escape characters while using grep command in Unix ?


    ReplyDelete
  32. grep -l command becomes savior for us as the line which contains error doesn't contain /r/n and wc -l is printing 0 when invoked as
    grep "FAILED" Message.xml | wc -l and we wanted to find total number of files which contains "FAILED" word, by running grep -l we were able to do that.

    grep -l "FAILED" *.xml | wc -l

    gives us total number of file which contains FAILED word even on the lines which doesn't contain /r/n

    ReplyDelete
  33. How does one exclude multiple keywords?

    ReplyDelete
  34. If you look man page of egrep or fgrep than you find that use of egrep and frep is deprecated and instead of using egrep you can use grep -E where E is for extended regular expression, similarly instead of grep you can use grep -F, where F stands for fixed string. One of my favorite grep command example is searching for multiple keywords using grep -E as some time they are related like parent and child or status like NEW CXL EXEC etc.

    grep -E "NEW|CXL|EXEC" status.log
    this will print all lines which contains either NEW, CXL or EXEC keywords in status.log file.

    ReplyDelete
  35. Well Said @Steven. In fact I am also big fan of egrep multiple word searching capability. If you have enabled color coding for grep, egrep and fgrep in your .bashrc, or explicitly using them by providing --color option, you can use egrep to see existence of any word. For example, if you are working with FIX based Java application and have to analyse and debug FIX logs daily, you can use egrep to see if your order or execution report contains a particular tag or not. This is one task, I found we do repeatedly throughout the day, but by using colored egrep saves lot of time. For example, I am using egrep to check if Execution report contains tag 150 or not, and with value = F (Trade).

    egrep '150=F|151=' app.log

    This will highlight both tags in your egrep output, which means you can not only check if tag exists but their value as well.

    ReplyDelete
  36. You can avoid piping grep into another grep when 'grep' itself is part of the output string. Consider following example:

    ps -ef | grep 'HTTP' | grep -v 'grep'

    You can achieve same effect by using regular expression with grep command like this

    ps -ef | grep '[H]TTP'

    ReplyDelete
  37. One of the useful grep command option which I would like to add is "grep -o" where o is for only matching and shows the part of line which matches the given pattern. One example of this to print status code from Apache server's log as shown below :

    $ grep -o " [0-9]{3} " /var/log/apache2/access.log
    503
    404
    404
    200
    200
    200

    I love grep, its not just a tool, its the best friend of developer :-)

    ReplyDelete
  38. Can Anyone help me to capture date wise log using grep command with example

    ReplyDelete
  39. @Sachin, why not just grep with the date format you are using e.g. if you are using DDMMYYY just use grep '01022016' abc.log.

    ReplyDelete