In this guide, we’ll have a quick look at some of the most common implementations of the grep command.
The grep command structure
Any grep command will have the following components.
Common grep syntaxes
Searching for a string in a file
This is the simplest way of using the grep command. In the following example, grep will search for the term “dkms” (without quotes) in the file apt-packages-installed.txt (containing all the APT packages installed in the system).
It’s recommended to use double quotations (“”) to indicate a string because a string may contain special characters and whitespaces.
Searching for a string in multiple files
The grep command can also perform the searches on multiple files. All you have to do is specify the files sequentially.
Instead of manually specifying the files, you can also use the wildcard (*) expression.
What if we wanted to perform the search on all the files in the current directory? Just set the wildcard expression to indicate every file in the current directory.
As we can see, grep found all the matches in the text files but didn’t handle the directories well. This perfectly leads to the next point.
By default, grep won’t search the subdirectories for matches. If you want to grep to do so, you have to tell it by adding the flag “-r.”
Try performing the previous search once again with this new trick.
Note that given the layers of subdirectories and the number of files to sift through, grep can take some time. On-screen, it will appear as if it’s frozen. So, don’t panic if you face similar situations.
Checking the line number of the match
Having the line number of the grep match can be a godsend. To see the line number of the match, use the flag “-n.”
Counting number of matches
The grep command can count the number of matches in the search result. To do so, use the flag “-l.”
Here, grep will show the number of matches for each searched file.
Print name of matching files
Sometimes, you want to know which files matched the pattern specified without the search result. If that’s the case, use the flag “-l.”
Print lines before and after the matching string
By default, grep will print the line it finds the specified string. However, we can calibrate the output to print a couple of lines before and/or after the string match.
Grep will print the matching string along with 5 previous lines on the console in the following command, thanks to the “-B” flag.
Similarly, we can also tell grep to print several lines after finding the matching string. To do so, we’ll use the flag “-A.” In the following example, grep will print 5 lines after finding the match.
Now, it’s time to combine both of these features.
The command becomes unnecessarily confusing in this manner. We can tell grep to print several before and after the match is found to solve this. To do so, we’ll use the flag “-C.”
Note that the output will be symmetrical around the matching string if you’re using the “-C” flag. If you want an asymmetric number of lines, you have to separately describe them using the “-A” and “-B” flags.
Limiting grep output
If you’re working with a large text file that contains many instances of the search pattern, then the grep output will print a big chunk of output with lots of mess. For example,
We can tell grep to limit the number of lines it’s allowed to output in such a situation. To do so, we’ll use the flag “-m.” The command will look like this:
Here, grep will only print the first 10 of the matching patterns.
Using grep with STDOUT
The STDOUT is a file stream. It’s a powerful feature of shells (like Bash). It’s a temporary file that contains the output of the previous command. When the next command is run, the value of STDOUT is updated. Learn more about STDIN, STDERR, and STDOUT in Bash.
The grep command can also work with STDOUT content. For example, you can pipe the output of a command for grep to work on.
In the following example, the APT command returns the list of all the packages installed in the system. We pipe the output to grep and search for the term “dkms” in the output. According to grep‘s default behavior, it will print all the lines that contain the string.
The output may warn that using APT in scripts isn’t recommended as APT doesn’t offer a stable CLI interface. Learn more about the APT package manager on Ubuntu.
This is a brief guide on some of the most common usages of the grep command. For mastering grep, it’s recommended to check out this mega guide demonstrating 30 grep examples.
The help page of grep prints a short page of all the available options.
The man page of grep contains all the options with detailed explanations.
For beginners, this guide on grep command in Linux is an excellent starting point.