Practical Example of grep command in unix


Published on

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 contains some of most useful example of grep command in unix.

for more tutorial see my blog

Published in: Technology, Education
  • Njce! Thanks for sharing.
    Are you sure you want to  Yes  No
    Your message goes here
  • Great presentation Mr. Javin. Good job and thanks for sharing it with us. I would like to clarify one thing though, that:
    grep -c 'Error' logfile.txt
    does not tell you how many 'times' the word 'Error' occurs in logfile.txt, but the number of 'lines' on which word 'Error' is found in the logfile.txt. That is, if the word 'Error' appears multiple times on the same line, it will be counted only once.
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Practical Example of grep command in unix

  1. 1. How to use Grep command in Unix or Linux
  2. 2. Grep command in Unix <ul><li>This tutorial is about how to use grep command in unix and here we will see some practical example of using grep in Unix </li></ul><ul><li>&quot;grep&quot; one of the most frequently used UNIX command stands for &quot;Global Regular Expression Print&quot;. 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. </li></ul>
  3. 3. How people use Grep in Unix <ul><li>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. </li></ul>
  4. 4. 10 examples of grep command in UNIX and Linux <ul><li>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…………. </li></ul>
  5. 5. Finding matching word and excluding unwanted <ul><li>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 </li></ul><ul><li>grep Exception logfile.txt | grep -v ERROR </li></ul>
  6. 6. Counting number of matching word <ul><li>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 &quot;Error&quot; has appeared in logfile.txt </li></ul><ul><li>grep -c &quot;Error&quot; logfile.txt </li></ul>
  7. 7. Displaying Context around matching word <ul><li>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 &quot;successful&quot; in logfile.txt </li></ul><ul><li>grep --context=6 successful logfile.txt </li></ul><ul><li>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 &quot;C&quot; instead of &quot;--context&quot; for example </li></ul><ul><li>grep -C 2 'hello' * </li></ul><ul><li>Prints two lines of context around each matching line. </li></ul>
  8. 8. Using EGrep for sophisticated search <ul><li>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 &quot;|&quot; option to search for either Error or Exception by executing just one command. </li></ul><ul><li>grep -i Error logfile </li></ul>
  9. 9. Using Zgrep to search in gzip file <ul><li>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 &quot;Error&quot; on them. </li></ul><ul><li>zgrep -i Error *.gz </li></ul>
  10. 10. Finding whole word using grep in Unix Linux <ul><li>7) Use grep -w command in UNIX if you find whole word instead of just pattern. </li></ul><ul><li>grep -w ERROR logfile </li></ul><ul><li>Above grep command in UNIX searches only for instances of 'ERROR' that are entire words; it does not match `SysERROR'. </li></ul><ul><li>For more control, use `<' and `>' to match the start and end of words.  For example: </li></ul><ul><li>grep 'ERROR>' * </li></ul><ul><li>Searches only for words ending in 'ERROR', so it matches the word `SysERROR'. </li></ul>
  11. 11. Display only file names of matching String <ul><li>8) Another useful grep command line option is &quot;grep -l&quot; which display only the file names which matches the given pattern. Below command will only display file names which have ERROR? </li></ul><ul><li>grep -l ERROR *.log </li></ul><ul><li>grep -l 'main' *.java will list the names of all Java files in the current directory whose contents mention `main'. </li></ul>
  12. 12. Displaying line number of matches using grep in unix <ul><li>9) If you want to see line number of matching lines you can use option &quot;grep -n&quot; below command will show on which lines Error has appeared. </li></ul><ul><li>grep -n ERROR log file. </li></ul>
  13. 13. Recursive search using grep in Unix <ul><li>10) If you want to do recursive search using grep command in Unix there are two options either use &quot;-R&quot; command line option or increase directory one by one as shown below. </li></ul><ul><li>Grep –R HelloWorld * </li></ul><ul><li>Or </li></ul><ul><li>Grep HelloWorld */* </li></ul><ul><li>Grep HelloWorld */*/* </li></ul><ul><li>Everytime you put an aditional * it will search one level down. </li></ul>
  14. 14. Now I have two bonus examples of grep command in unix: <ul><li>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. </li></ul><ul><li>grep Exception  today.log --color </li></ul><ul><li>12) at last there are three version of grep command in UNIX `grep, fgrep, egrep'. `fgrep' stands for Fixed `grep', `egrep' Extended `grep‘ use it based on your need. </li></ul>
  15. 15. Summary <ul><li>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. </li></ul>
  16. 16. Author <ul><li>Name: Javin Paul </li></ul><ul><li>Website: </li></ul><ul><li>Javin Paul is an expert in the area of Java, Unix and TIBCO RV and and has been working with these technology from past 7 years while working in various project in finance and trading domain. </li></ul>