In this tutorial, we are going to learn about grep command usage with examples in Linux. 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 "egrep" and "fgrep", respectively. Without further ado, let us get started.
Table of Contents
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
Sample output:
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
Sample output:
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 Ostechnix
$ 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
Understanding Grep Context Flags
Sometimes, you may want to see not just the matching line but also the lines around it. This is where the context flags come in: --context
, -A
, -B
, and -C
. Using grep context flags, you can easily see important surrounding information. For more details, refer to the following guide:
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, and 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
Or,
$ egrep '^([l-u]|[L-U])' file.txt
Sample output:
ostechnix Ostechnix o$technix linux linus unix technology
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
Suggested Read:
Etymology of grep
Grep was written overnight by Ken Thompson, the same guy who wrote Unix. But why and how did it get its name? Professor Brian Kernighan explains the etymology of grep in this video.
And, that's all. Grep is one of the important command that you should learn thoroughly.
You know now what is grep and how to use it to search text files with matching patterns in GNU/Linux. Also you learned how Grep got its name. Hope this was useful.
Related Read:
8 comments
You might want to check over your range syntax for matching the H and h characters.
egrep ‘^([l-u]|[L-U])’ file.txt will not find the letter ‘h’ or ‘H’. Just letting you know so you can correct it.
I will check and update the guide soon. Thanks.
Also, .n will match any character that precedes the letter ‘n’
fgrep does not stand for “fast grep”, but for “fixed grep”
Anyways, thanks for the guide 🙂
You could make examples demonstrating extended regular expressions slightly more consice. Notice that:
“^(l|o)” is equal to “^[lo]”, and ^([l-u]|[L-U])” is equal to “^[l-uL-U]” Although, for the latter expression, if you use the “i” option you can further simplify the initial command to just “egrep -i ^[l-u] “.
Noted. I will try to add more concise examples in the day to come. Thanks for the comment.
error here ^([l-u]|[L-U])’ not list “Hello world” because this ^ start string. List this egrep ‘([L-U]|[l-u])’
Good catch. I just updated the guide. Thanks for pointing it out.