

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. In zsh -o extendedglob where ^ is a globbing operator, ^ would mean any file but p, q or s.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. If there's no matching file, in csh, tcsh, zsh or bash -O failglob, you'll get an error message and the command will be cancelled. So in those shells like bash that don't treat ^ specially, if there is a file called ^p in the current directory, that will become ls /usr | grep ^p That means that ^ is meant to be expanded by the shell to the list of files that match that pattern (relative to the current directory). Then, in most shells ( fish being a notable exception), is a globbing operator. First ^ is a special character in a few shells like the Bourne shell, rc, es or zsh -o extendedglob (though OK in bash or other POSIX shells). Since you only want those that start with p, q or s, that's redundant. a is to include hidden files, that is files whose name starts with. Generally, you can't post-process the output of ls reliably. So that command doesn't return the files whose name starts with p, q or s, but the lines of the filenames that start with p, q or s. That's probably what your teacher is expecting but it's wrong or at least not reliable.įile names can be made of many lines since the newline character is as valid a character as any in a file name on Linux or any unix. Would select from the output of ls -a /usr (which is the list of files in /usr delimited by newline characters) the lines that start by either of the p, r or s characters.
