In this tutorial, we are going to learn about "grep" command. Grep stands for Global regular expression print. As the name implies, Grep is used to search text files with regular expressions(shortly regex). It prints the lines matching the given pattern in a text file. If no file is given, grep will recursively search the given pattern in the files in current directory. Grep has two variants, namely egrep and fgrep. These variants are deprecated but are provided for backward compatibility. Instead of using "grep -E" and "grep -F", you can use "egrp" and "fgrep", respectively. Without further ado, let us get started.
Grep Command examples
I have a file named file.txt with some random words. Let us have a look at file.txt:
$ cat file.txt ostechnix Ostechnix o$technix linux linus unix technology hello world HELLO world
To begin the search, just type grep followed by what it is you're looking for and where you are looking from. For example, I am going to look for the string "nix" in file.txt. To do so, I run:
$ grep nix file.txt
ostechnix Ostechnix o$technix unix
As you see in the above output, we got two words that contains the matching pattern "nix". If the search string has two words, mention them inside single quotes like below.
$ grep 'hello world' file.txt hello world
You can also use -n flag to show the line numbers in the output:
$ grep -n nix file.txt
1:ostechnix 2:Ostechnix 3:o$technix 6:unix
This can be useful when you're working with a really long code.
Please note that grep is case-sensitive. For example, when you search for "os", it is not going to display lines the contains uppercase letters i.e Os.
Check the following example.
$ grep os file.txt ostechnix
But, I have another word named "Ostechnix" in file.txt, but grep didn't list it.
If you want, however, to ignore case sensitive, you can use "-i" flag like below.
$ grep -i os file.txt ostechnix Ostechn1x
$ grep -i 'hello world' file.txt hello world HELLO world
Now, grep didn't care about the case and we got the words that contains both uppercase and lowercase letters in the result.
We can also pipe an output of a command into grep. Have a look at the following example.
$ cat file.txt | grep os ostechnix
Now see what we've got. The output of the file.txt is piped into grep and the words that contains the letters "os" in file.txt have been displayed.
We can also use some special characters or regular expressions in grep.
- ^ - search at the beginning of the line.
- $ - search at the end of the line.
- . - Search any character.
Let me show you an example, so you can understand where and how to use those special characters.
You know already, we can search for the words that contains the string "tech" like below.
$ grep tech file.txt ostechnix Ostechnix o$technix technology
But what if you just wanted to search for the lines that only start with the word "tech". You don't want to display all the words that contains the string, but only the words that have the string "tech" at the beginning. It is also possible. This is where special characters comes in handy.
To search for the words that matches the pattern "tech" at the beginning of the line in a file, run:
$ grep ^tech file.txt technology
Similarly, we can search for the words that ends with a particular letter(s), for example "x", like below.
$ grep x$ file.txt ostechnix Ostechnix o$technix linux unix
Also, you can find the words that contains any character using . (dot).
For example, let us search for any word that has "n" in the file.
$ grep .n file.txt ostechnix Ostechnix o$technix linux linus unix technology
You should now have a basic understanding of grep usage. Let us go ahead and learn the other two variants, namely egrep and fgrep.
Egrep command examples
egrep stands for extended grep. It is similar to "grep -E" command. It will do all the things that grep will do. However, It provides some additional functionalities, such as using complicated regex, than the normal grep command does out of the box.
For instance, we can search for any words that start with either "l" or "o".
Remember we use caret symbol (^) to search words at the beginning of line. Hence, our command for the above query will be:
$ egrep '^(l|o)' file.txt ostechnix o$technix linux linus
See? We have got all of the words that starts with either "l" or "o". Please note that the normal grep command can't do this.
Similarly, We can search for the lines that start with any character range between "l" to "u". That means, we will get the lines that start with l, m, n, o, p, q, r, s, t, u. Everything else will be omitted from the result.
$ egrep '^[l-u]' file.txt ostechnix o$technix linux linus unix technology
Please note that I have used square bracket ([) to search for the range of words. Since grep is case-sensitive, it is not going to find lines that starts with uppercase letters in the given range.
To display all the lines that starts with both upper and lower case letters, we use:
$ egrep '^[l-u]|[L-U]' file.txt
$ egrep '^([l-u]|[L-U])' file.txt
ostechnix Ostechnix o$technix linux linus unix technology HELLO world
See? Now we have got the words that begins with the character range "l" to "u", either upper or lower case.
Fgrep command examples
fgrep stands for fast grep. It is similar to "grep -F". fgrep can't recognize regular expressions or special characters. fgrep can be used where you want regular expressions to be evaluated.
For example, we use the following command to find the words end in "x".
$ grep x$ file.txt ostechnix Ostechnix o$technix linux unix
Now run the same command with fgrep.
$ fgrep x$ file.txt
It will display nothing, because it couldn't evaluate the special characters.
Now let us see another example. To search for the words that matches the string "o$" with grep command, we do:
$ grep o$ file.txt
But, we get nothing, why? Because, as per the above command, we use the dollar symbol($) to find the words that ends in "o". Since there were no characters ends with "o" in file.txt, we get nothing.
Now, run the same command with fgrep.
$ fgrep o$ file.txt o$technix
See? This is where we use fgrep command. It simply ignores the dollar symbol (the special character, of course) and displays the word that contains the string "o$".
For more details about grep, type:
$ grep --help
It will give all possible options. Or, refer the man pages.
$ man grep
And, that's all. You know now what is grep and how to use it to search text files with matching patterns in GNU/Linux. Grep is one of the important command you should learn thoroughly.
More good stuffs to come. Stay tuned!