securitybreach Posted December 8, 2014 Share Posted December 8, 2014 10 Examples of grep command in Linux grep command examples in Unix and Linux grep command is one of the most frequently used UNIX command stands for "Global Regular Expression Print" like find, chmod or tar command in Unix. grep command in Unix operating system e.g. Linux, Solaris, BSD, Ubuntu or IBM AIX is used to search files with matching patterns, by using grep command in Unix you can search a file which contains a particular word or particular pattern. UNIX grep command also provides several useful command line option which can be used to enhance functionality of grep command e.g. by using grep -v you can list down all files which doesn't contains a word i.e. excluding files which matches a pattern, grep -c will print count of matching pattern in a file etc. One of the popular example of grep command is to find empty files and directories in Unix. This grep command tutorial is not about theory of UNIX grep but practical use of grep command 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. 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. If you want to leverage full potential of grep, then using Grep pocket reference is not a bad idea, an ideal grep reference for system admin, developers and security professionals. Following examples on grep command in UNIX are based on my experience and I use them on daily basis in my work. Grep command is also part of any beginners UNIX command tutorial as 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. Any way 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. grep command in Unix Example 1: Finding relevant word and exclusion of irrelevant word. Most of the time I look forException 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 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 contains 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.............. http://javarevisited...n-unix-and.html 1 Quote Link to comment Share on other sites More sharing options...
crp Posted December 8, 2014 Share Posted December 8, 2014 i'd like to be able to use grep in an alias but can not figure out how to get the searched-for-term into the grep command. Quote Link to comment Share on other sites More sharing options...
securitybreach Posted December 9, 2014 Author Share Posted December 9, 2014 Well since grep requires a term to output, I do not know if it would be possible unless you always wanted to grep the same text from every search. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.