The "global regular expression print" or "grep" for short is a text processing editor. The command's primary purpose is searching for a specific pattern that can be either an integer, a string, or a combination of both and printing out the result. It is useful when you are looking through log files, searching for a specific entry, or you want to find a unique line or piece of code located somewhere in your files. As the possibilities this command offers are practically limitless, we cannot cover all of them in this article. We are going to teach you the standard practices you may use in your journey to master it and become a "grep" ninja!
The syntax of the command itself looks like this:
grep <flags> <pattern> <file>
If you want to search through all of the files inside a directory, you do not need to define a file - just run the command as:
grep <flags> <pattern>
Let's start with a simple example. We have 4 test files inside the directory we are currently residing:
-rw-r--r-- 1 root root 10 Mar 12 14:43 test1 -rw-r--r-- 1 root root 68 Mar 12 14:44 test2 -rw-r--r-- 1 root root 11 Mar 12 14:43 test3 -rw-r--r-- 1 root root 21 Mar 12 14:43 test4
The content of these files is as follows:
test1 - I am a grep ninja test2 - Mar 12 14:35:01 ger1 systemd: Started Session c338992 of user root. test3 - HelloWorld test4 - [email protected]
Let's say we need to find the pattern "ninja" inside the files within this directory without having to open all of them one by one. To do that, we are going to use a few flags for our grep command which we are going to explain:
[[email protected] grep-tutorial]# grep -ri "ninja" test1:ninja
As you can see, the command displayed the file along with the word "ninja". Here are the options we used:
- R - This flag stands for recursively. It allows you to search within the folders located in the directory if such exist.
- i - This flag allows you to ignore letter capitalization and find patterns, despite there being a capital letter inside them.
- o - This option allows you to print only the word you are searching for within a file, excluding the rest of the words.
Ok, let print out only the file, which contains the pattern "ninja" without the actual string. To do so, replace the "o" option with the "l" flag. This modification is going to generate a list of the file/s containing this specific word.
[[email protected] grep-tutorial]# grep -Ril "ninja" test1
To demonstrate the other functions of grep, we created a new file "text5" with the following content:
[[email protected] grep-tutorial]# cat test5 I am a grep ninja grep is an amazing tool grep while greping for grep as you grep for grep. GrEp is VerYpoWerful if usEdprOperly!
Using grep, you can find the number of lines in which the pattern is present. To do this, we are going to be using the "c" flag, which stands for "count", and it is going to show us exactly what we need.
[[email protected] grep-tutorial]# grep -ci "grep" test5 4
Another useful option of the grep is the "w" flag. It is going to enable "grep" to print a match ONLY if the pattern matches correctly.
[[email protected] grep-tutorial]# grep -i "gre" test5 I am a grep ninja grep is an amazing tool grep while greping for grep as you grep for grep. GrEp is VerYpoWerful if usEdprOperly!
In this case, "grep" prints out the lines, as the "gre" is a part of the word. However, when we add the "w" option, here is what happens:
[[email protected] grep-tutorial]# grep -iw "gre" test5 [[email protected] grep-tutorial]#
You are going to see an empty output because "gre" does not match perfectly with anything written in the files.
The grep utility is also going to allow you to remove the pattern from the output completely, making it print out the lines in which the pattern is not present. This functionality can be achieved with the "v" flag:
[[email protected] grep-tutorial]# grep -v "grep" test5 GrEp is VerYpoWerful if usEdprOperly!
Despite the fact that "grep" happens to be stored on the previous lines, it is excluded from the output.
The last example we are going to give is regarding regular expressions. These are specific characters or sequence of characters that have a special meaning within search patterns allow you to achieve a more precise result.
[[email protected] grep-tutorial]# grep -e "^grep" test5 grep is an amazing tool grep while greping for grep as you grep for grep.
To allow "grep" to recognize regular expressions, you must put the "-e" flag. In the example above, we have used the "^" regex, which instructs the command to show ONLY the lines that START with the pattern "grep".
In the next example, we are going to use the "$" regex, instructing the command to show us ONLY the lines that END with the word "grep".
[[email protected] grep-tutorial]# grep -e "grep$" test5 grep while greping for grep as you grep for grep
We are going to include a bonus command which is going to allow us to demonstrate how powerful "grep" truly is:
[[email protected] grep-tutorial]# grep -irh "\w*@\w*.\w*" [email protected]
The regular expressions we have between the quotation marks attempts to capture the pattern email addresses use, which is [email protected]. This command is going to show ALL of the email addresses listed within the files inside a specific folder.