Upgrade to Pro — share decks privately, control downloads, hide ads and more …

10 ways to use Grep comand in UNIX

Javin Paul
August 16, 2013

10 ways to use Grep comand in UNIX

Grep is one of the useful command in UNIX, but most of users are not aware of the full power of grep. This tutorial explains 10 most useful examples of grep command in UNIX based system e.g. Linux, Solaris etc. See here for more example http://javarevisited.blogspot.com/2011/06/10-examples-of-grep-command-in-unix-and.html.

@javinpaul
http://java67.blogspot.com

Javin Paul

August 16, 2013
Tweet

Other Decks in Programming

Transcript

  1. How to use How to use Grep Grep command command

    in Unix or Linux in Unix or Linux
  2. Grep Grep command in Unix command in Unix This tutorial

    is about how to use This tutorial is about how to use grep grep command in command in unix unix and here we will see some practical example and here we will see some practical example of using of using grep grep in Unix in Unix " "grep grep" " one of the most frequently used UNIX command one of the most frequently used UNIX command stands for "Global Regular Expression Print". This stands for "Global Regular Expression Print". This grep grep command tutorial is not about theory of command tutorial is not about theory of UNIX UNIX grep grep but to but to practical use of practical use of grep grep in UNIX and here I am sharing my in UNIX and here I am sharing my experience on use of experience on use of grep grep command in Linux command in Linux with an with an aim that this would serve as quick guide or tutorial for aim that this would serve as quick guide or tutorial for using using grep grep in UNIX for new beginners and help them to in UNIX for new beginners and help them to understand the understand the grep grep command better and its thoughtful command better and its thoughtful usage in UNIX or Linux. usage in UNIX or Linux.
  3. How people use How people use Grep Grep in Unix

    in Unix Many people use Many people use grep grep just for finding words just for finding words in a file and missed the real potential of in a file and missed the real potential of grep grep by not using all its powerful command by not using all its powerful command line options and its regular expression line options and its regular expression capability which could not only save a lot of capability which could not only save a lot of time but also works as a great and powerful time but also works as a great and powerful tool while analyzing large set of data or log tool while analyzing large set of data or log files. files. Also Also find command in UNIX find command in UNIX can be can be used in place of used in place of grep grep at many places. at many places.
  4. 10 examples of 10 examples of grep grep command command

    in UNIX and Linux in UNIX and Linux Following Following examples on examples on grep grep command in command in UNIX UNIX are based on my experience and I use are based on my experience and I use them on daily basis in my work. These them on daily basis in my work. These examples are by no means complete so examples are by no means complete so please contribute your please contribute your grep grep command tips command tips or how you are using or how you are using grep grep in Linux to make in Linux to make it more useful and allow all of us to benefit it more useful and allow all of us to benefit from each others experience and work from each others experience and work efficiently in UNIX or Linux. efficiently in UNIX or Linux. So here we go So here we go………… …………. .
  5. Finding matching word and Finding matching word and excluding unwanted

    excluding unwanted 1) Finding relevant word and exclusion irrelevant 1) Finding relevant word and exclusion irrelevant word. Most of the time I look for Exception and word. Most of the time I look for Exception and Errors in log files and some time I know certain Errors in log files and some time I know certain Exception I can ignore so I use Exception I can ignore so I use grep grep - -v option to v option to exclude those Exceptions exclude those Exceptions grep grep Exception Exception logfile.txt logfile.txt | | grep grep - -v ERROR v ERROR
  6. Counting number of matching word Counting number of matching word

    2) If you want to count of a particular word 2) If you want to count of a particular word in log file you can use in log file you can use grep grep - -c option to c option to count the word. Below command will print count the word. Below command will print how many times word "Error" has appeared how many times word "Error" has appeared in in logfile.txt logfile.txt grep grep - -c "Error" c "Error" logfile.txt logfile.txt
  7. Displaying Context around matching Displaying Context around matching word word

    3) Sometime we are not just interested on matching line but also 3) Sometime we are not just interested on matching line but also on on lines around matching lines particularly useful to see what happ lines around matching lines particularly useful to see what happens ens before any Error or Exception. before any Error or Exception. grep grep -- --context option allows us to print context option allows us to print lines around matching pattern. Below example of lines around matching pattern. Below example of grep grep command in command in UNIX will print 6 lines around matching line of word "successful UNIX will print 6 lines around matching line of word "successful" in " in logfile.txt logfile.txt grep grep -- --context=6 successful context=6 successful logfile.txt logfile.txt Show additional six lines after matching very useful to see what Show additional six lines after matching very useful to see what is is around and to print whole message if it splits around multiple l around and to print whole message if it splits around multiple lines. ines. You can also use command line option "C" instead of " You can also use command line option "C" instead of "-- --context" for context" for example example grep grep - -C 2 'hello' * C 2 'hello' * Prints two lines of context around each matching line. Prints two lines of context around each matching line.
  8. Using Using EGrep EGrep for sophisticated search for sophisticated search

    4) 4) egrep egrep stands for extended stands for extended grep grep and it is and it is more powerful than more powerful than grep grep command in Unix command in Unix and allows more regular exception like you and allows more regular exception like you can use "|" option to search for either Error can use "|" option to search for either Error or Exception by executing just one or Exception by executing just one command. command. grep grep - -i Error i Error logfile logfile
  9. Using Using Zgrep Zgrep to search in to search in

    gzip gzip file file 6) 6) zgrep zgrep is another great version of is another great version of grep grep command in Unix which is used to perform same command in Unix which is used to perform same operation as operation as grep grep does but with . does but with .gz gz files. Many a files. Many a times we times we gzip gzip the old file to reduce size and later the old file to reduce size and later wants to look or find something on those file. wants to look or find something on those file. zgrep zgrep is your man for those days. Below command is your man for those days. Below command will print all files which have "Error" on them. will print all files which have "Error" on them. zgrep zgrep - -i Error *. i Error *.gz gz
  10. Finding whole word using Finding whole word using grep grep

    in in Unix Linux Unix Linux 7) Use 7) Use grep grep - -w command in UNIX if you find whole word instead of w command in UNIX if you find whole word instead of just pattern. just pattern. grep grep - -w ERROR w ERROR logfile logfile Above Above grep grep command in UNIX searches only for instances of 'ERROR' command in UNIX searches only for instances of 'ERROR' that are entire words; it does not match ` that are entire words; it does not match `SysERROR SysERROR'. '. For more control, use ` For more control, use `\ \<' and ` <' and `\ \>' to match the start and end of >' to match the start and end of words. words. For example: For example: grep grep 'ERROR>' * 'ERROR>' * Searches only for words ending in 'ERROR', so it matches the wor Searches only for words ending in 'ERROR', so it matches the word d ` `SysERROR SysERROR'. '.
  11. Display only file names of matching Display only file names

    of matching String String 8) Another useful 8) Another useful grep grep command line option is command line option is " "grep grep - -l" which display only the file names which l" which display only the file names which matches the given pattern. Below command will matches the given pattern. Below command will only display file names which have ERROR? only display file names which have ERROR? grep grep - -l ERROR *.log l ERROR *.log grep grep - -l 'main' *.java will list the names of all Java l 'main' *.java will list the names of all Java files in the current directory whose contents files in the current directory whose contents mention `main'. mention `main'.
  12. Displaying line number of matches Displaying line number of matches

    using using grep grep in in unix unix 9) If you want to see line number of 9) If you want to see line number of matching lines you can use option " matching lines you can use option "grep grep - -n" n" below command will show on which lines below command will show on which lines Error has appeared. Error has appeared. grep grep - -n ERROR log file. n ERROR log file.
  13. Recursive search using Recursive search using grep grep in Unix

    in Unix 10) If you want to do recursive search using 10) If you want to do recursive search using grep grep command in Unix there are two options either use command in Unix there are two options either use " "- -R" command line option or increase directory R" command line option or increase directory one by one as shown below. one by one as shown below. Grep Grep – –R R HelloWorld HelloWorld * * Or Or Grep Grep HelloWorld HelloWorld */* */* Grep Grep HelloWorld HelloWorld */*/* */*/* Everytime Everytime you put an you put an aditional aditional * it will search one * it will search one level down. level down.
  14. Now I have two bonus examples Now I have two

    bonus examples of of grep grep command in command in unix unix: : 11) 11) grep grep command in UNIX command in UNIX can show matching patter can show matching patter in color which is quite useful to highlight the matching in color which is quite useful to highlight the matching section , to see matching pattern in color use below section , to see matching pattern in color use below command. command. grep grep Exception Exception today.log today.log -- --color color 12) at last there are three version of 12) at last there are three version of grep grep command in command in UNIX ` UNIX `grep grep, , fgrep fgrep, , egrep egrep'. ` '. `fgrep fgrep' stands for Fixed ` ' stands for Fixed `grep grep', ', ` `egrep egrep' Extended ` ' Extended `grep grep‘ ‘ use it based on your need. use it based on your need.
  15. Summary Summary These These examples of examples of grep grep

    command in UNIX command in UNIX are are something which I use on daily basis; I have seen more something which I use on daily basis; I have seen more sophisticated use of sophisticated use of grep grep with regular expression. I will list with regular expression. I will list some more examples of some more examples of grep grep command in UNIX as I come command in UNIX as I come across and find useful to share. As per my experience across and find useful to share. As per my experience having good hold on having good hold on grep grep and and UNIX find command UNIX find command with with knowledge of regular expression will be great for you day knowledge of regular expression will be great for you day to day life if you need to look log files or to day life if you need to look log files or config config files or files or need to do production support on electronic trading need to do production support on electronic trading systems or any other kind of system which is running on systems or any other kind of system which is running on UNIX. This list of UNIX. This list of grep grep command in UNIX is by no means command in UNIX is by no means complete and I look forward from you guys to share how complete and I look forward from you guys to share how you are using you are using grep grep command in UNIX. command in UNIX.
  16. Author Author Name: Name: Javin Javin Paul Paul Website: Website:

    http://javarevisited.blogspot.com/2011/06/10 http://javarevisited.blogspot.com/2011/06/10- - examples examples- -of of- -grep grep- -command command- -in in- -unix unix- -and.html and.html Javin Javin Paul is an expert in the area of Java, Unix Paul is an expert in the area of Java, Unix and TIBCO RV and and has been working with and TIBCO RV and and has been working with these technology from past 7 years while working these technology from past 7 years while working in various project in finance and trading domain. in various project in finance and trading domain.