

Prints two lines of context around each matching line.Įxample 4: How to search pattern using egrep and regular expression You can also use command-line option "C" instead of "-context" for example 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. 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".īelow an example of grep command in UNIX will print 6 lines around the matching line of word "successful" in logfile.txt Grep Exception logfile.txt | grep -v ERROR 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 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.Īnyway, 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.Įxample 1: How to ignore some words while doing a search using grep in UNIXįinding relevant words and exclusion of irrelevant words. The following examples of grep command in UNIX are based on my experience and I use them on a daily basis in my work. It's also very affordable and you can buy in just $10 on Udemy flash sales which happen every now and then.ġ0 ways to use Grep command in Unix - examples It's a very practical and hands-on course to learn Linux fundamentals in a quick time.

If you need an online course, I highly recommend Linux Mastery: Master the Linux Command Line in 11.5 Hours on Udemy.

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.īy 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.

#Grep examples line number code#
You'll see a lot of StackOverflow answers recommending -color=always, but that will give you those weird color code characters and is not probably what you want.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. This is because the colors are coded as characters right into the output text, and they'll show up in plaintext output files and other programs like less. "Auto" will colorize the output if it is being output to a terminal, but drop the colors if the output is getting piped to another command or a file. In fact, there's even a -colour convenience option for those so inclined! It can take the values "never", "always", or "auto". I love vim and use this feature all the time. Vim users: YoU cAn Do ThE sAmE tHiNg In ViM! If you type /magic, you see something like this: Run less example.txt and then type / followed by your search term. A better way might be to use the neat search functions of less. Enter fullscreen mode Exit fullscreen modeĪlthough, that's abusing grep a little bit.
