Have you used grep before? Most of the time, the basic grep trick can do most of the work. However, grep offers tons of ways to perform the search and fine-tune the output to a more usable version. In this article, let’s check out the usage of grep command.
Grep usage
Verifying existence
If you’re running any sort of Linux distro, then you already have grep installed. Run the following command in the terminal.
This command is used to show the version of currently installed grep. Now, we need a demo file. In my case, I’ll be generating a text file that includes all the installed packages on my system.
Basics
The basic usage of grep follows the following structure.
Or, for easier understanding, use this one.
In this case, grep will perform a search in the file and print all the lines that include the pattern (search term).
Grep searched the file “PackageList.txt” I generated earlier and printed all the lines that include “python”.
This same operation can be performed in another way. Check out the following example.
Here, using “cat” command, I sent the content of the file “PackageList.txt” to grep. Using the output of cat, grep performed the search and printed the lines that contain the search term.
Now comes a fun thing. You can literally stack multiple grep commands just like that.
The first grep filters down to the lines with a hyphen, the second filter down to the lines with p, and the final grep filters down to the lines with python. Makes sense?
Case sensitivity
When performing a search, case sensitivity is a major question. By default, grep is case sensitive.
For example, searching for “Python” won’t show any result.
To make grep case “insensitive”, add the following option.
File search
Let’s say that you have a directory with numerous text files. Your goal is to identify the file(s) that contain or doesn’t contain a pattern (search term).
I find this method quite helpful when searching within a pile of log files. As I don’t have the time to open and check every single file manually, I have grep to do the job for me.
For listing files containing the match, use the “-l” flag.
As the result suggests, the term “python” is present in all 3 files present in the “Desktop” directory.
For listing files without any match, use the “-L” flag.
“NoMatch.txt” is the only file that doesn’t contain the term “python”.
Inverted search
The default behavior of grep is to print only the lines with the matching pattern, right? It’s time to reverse the process. This time, we’ll be printing only the lines WITHOUT the matching pattern.
Just pass the “-v” option to grep.
Printing lines before/after the match
By default, grep will only print the line that matches the search pattern. Using this technique, you can tell grep to print lines before/after the match as well.
For printing lines before the match, use the following structure.
Here, 5 is the line of number that grep will print BEFORE the matching line.
For printing lines after the match, use the following one.
How about printing both before and after the matching line? In that case, use “-C” flag.
Line number
When grep shows the output, it doesn’t mention the line number. For the associated line number(s), use “-n” flag.
Single word
If the flag “-w” is used, grep will treat the pattern as a whole word.
Limiting grep search
Grep allows specifying the number of lines to search in the file. This method is useful if you’re dealing with a big file (like system log). Use the flag “-m”.
Recursive search
It’s one of the most helpful features grep offers for heavy usage. Grep can recursively search a directory and find all the matches from all the files it faces.
Or,
I often find using this recursive function along with “-l” flag.
Quiet mode
Grep can be run in “quiet” mode. When running in “quiet” mode, grep won’t print any output to the terminal. Instead, it will return 0 (at least, a match was found) or 1 (no match found).
echo $?
Regex
Grep also allows regex (regular expression) searches. This adds a whole new level of complexity and usability of grep as a searching tool.
For example, you can use brackets to search for both “too” and “two” at the same time.
This next example will only print the line if the match occurs at the very beginning of the line.
As for matching the ending, use this one.
If you want to use Perl regex, then use the “-P” flag. It will treat the pattern as Perl regex.
Final thoughts
Grep offers tons of ways to customize the search function. The availability of regex unlocks a whole new horizon for potential usage of grep. The cool thing is, you can use both general and Perl regex; whichever you feel comfortable with.
For the most detailed explanation, always consult the man page.
Cheers!