Grep Command in Linux
FREE Online Courses: Elevate Your Skills, Zero Cost Attached - Enroll Now!
In this document, you will learn how to use the grep command in Linux and why it is used. Go through most of the options that are used with grep command to search better, and also how to combine 2 or more commands, so that you can optimize and improve your searching techniques using the Grep command.
What is Grep command?
Grep command is a LINUX command which stands for Global regular expression print. The Grep command is used to search a file(s) for a word(s), sentence(s) or any pattern of words or characters and displays all the lines that contain the same.
Syntax:
grep <options> “word/sentence”<file(s)>
The syntax is pretty easy to understand: for the grep command we use any predefined options (which we will look into later) for more concise outputs. Then enter the word(s) or sentence(s) you want to search and the file(s) you want to search in.
Example:
grep “ferrari” car.txt
Output:
ferrari is a sports car manufactureferrari SP3 monza is their latest halo car
ferrari
Lamborghini is better than ferrari
The grep command used above searches for the word ‘ferrari’ in the file ‘cars.txt’ and prints all the sentences in which it finds the word “ferrari” . It prints the other outputs also (not only the 3rd output) because the word “ferrari” is in them too.
But what if we want to search only ‘ferrari’ or maybe even the line numbers it occurs in, or some context after and before the sentence the word is in? Well, that is why grep comes with a lot of predefined options to help us get a more accurate output. We can also combine 2 or more options to combine their functionalities and narrow down the output to exactly how we want it.
Let us look into the options of grep command in detail.
Before I go any further, let us take a standard text file to work on. For the rest of the article, I will be following the sample file cars.txt with the following content:
1: ferrari is a sports car manufacture
2: Enzo Ferrari is the founder.
3: the company is in Italy
4: ferrari SP3 monza is their latest halo car
5: which is extremely fast
6: with a 6.5L NA v12, producing 830 hp.
7: ferrari
9: Test words: FeRrArI, feRRari, ferrARI
10: The 458 italia is my favorite car of them
11: However I think,
12: Lamborghini is better than ferrari
13: Because I like their styling
14: Which is bold and striking!
15: Some of their cars also have scissor doors
Options used with Grep command
a. -i
Grep is a case sensitive command, which means that it will give the output of the exact case of what you typed. To make grep case insensitive (i.e. to output words like Ferrari, ferrari, FeRRari, ferRAI, etc) we use the option ‘-i’.
Syntax:
grep -i “word/sentence”<file(s)>
Example:
grep -i “ferrari” car.txt
Output:
ferrari is a sports car manufactureEnzo Ferrari is the founder.
ferrari SP3 Monza is their latest halo car
ferrari
Test words: FeRrArI, feRRari, ferrARI
Lamborghini is better than ferrari
b. -n
Sometimes you may want to know the line number a word or a sentence is in. For printing out the line numbers we use the option ‘-n’.
Syntax:
grep -n “word/sentence”<file(s)>
Example:
grep -n “ferrari” car.txt
Output:
1: ferrari is a sports car manufacture4: ferrari SP3 monza is their latest halo car
7: ferrari
12: Lamborghini is better than ferrari
c. -o
Notice in the previous 2 outputs, when we were searching for a word, we not only got the whole sentence the word was in. So if we want only the word and not the sentence it is in, we use the option ‘-o’.
Syntax:
grep -o “word/sentence”<file(s)>
Example:
grep -o “ferrari” car.txt
Output:
ferrariferrari
ferrari
ferrari
d. -c
The option -c gives us the count of the number of lines the word or sentence you have searched is in.
Syntax:
grep -c “word/sentence”<file(s)>
Example:
grep -c “ferrari” cars.txt
Output:
Sometimes if you are lost and want some context around the word/sentence you want to find, you can use the next 3 options (options 5, 6 and 7):
e. -B n
The option ‘-Bn’ is used to give n lines before the word that you are searching (note that this option will give n lines before the word for every result or match you get).
Syntax:
grep -B n “word/sentence”<file(s)>
Example:
grep -B 2 “Lamborghini” cars.txt
Output:
The 458 italia is my favorite car of themHowever I think,
Lamborghini is better than ferrari
The command gave me 2 lines before the sentence it found the word ‘Lamborghini’.
f. -A n
The option ‘-An’ is used to give n lines after the word that you are searching (note that this option will give n lines after the word for every result or match you get).
Syntax:
grep -A n “word/sentence”<file(s)>
Example:
grep -A 3 “Lamborghini” cars.txt
Output:
Lamborghini is better than ferrariBecause I like their styling
Which is bold and striking!
Some of their cars also have scissor doors
The command gave 3 lines after the sentence it found the word ‘Lamborghini’.
g. -C n
The option ‘-Bn’ is used to give n lines before and after the word that you are searching (note that this option will give n lines before the word for every result or match you get). This option is used to give a holistic picture as you get the content around your match.
Syntax:
grep -C n “word/sentence”<file(s)>
Example:
grep -c 2 “Lamborghini” cars.txt
Output:
The 458 italia is my favorite car of themHowever I think,
Lamborghini is better than ferrari
Because I like their styling
Which is bold and striking!
The command gave me 2 lines before and after the sentence it found the word ‘Lamborghini’.
h. -v
The option ‘v’ will print all the lines of the file which do not contain
the word/sentence you are searching for. It works like an invert filter.
Syntax:
grep -v “word/sentence”<file(s)>
Example:
grep -v “Lamborghini” cars.txt
Output:
This command will print out the entire document of cars.txt except for the line ‘Lamborghini is better than ferrari’.
i. -w
This option will match the whole word you want to search.
Syntax:
grep -w “word/sentence”<file(s)>
This is better understood with an example: (let me take a different example)
Example:
Suppose I have a document ‘names.txt’ with the following names: James William, James Williams, James Williamson and James williamsons.
Now If I run a regular grep command with no options:
grep “James William” names.txt
Output:
James WilliamJames Williams
James Williamson
James Williamsons
The output is given like this because there is the word ‘James William’ in all 4 names. Now this will is not an ideal method if we want to search names in a phone book or words in a dictionary, because the word we want to search might be a substring of another word (eg: bow and bowels or is and this).
So to avoid this mistake we use the option’-w’ to force the result to the exact word we want to search.
Now If I run the same grep command with ‘-w’ option:
grep -w “James William” names.txt
I will get the output as:
j. -I
The option ‘-I’ is used to display the files in which the word/sentence you want to search exists.
Syntax:
grep -I “word/sentence”<files>
Example:
Let us say that the ‘cars.txt’ file has been copied in multiple files cars1.txt, cars2.txt, cars3.txt
grep -I “Lamborghini” cars.txt cars1.txt cars2.txt
Output:
cars.txtcars1.txt
cars2.txt
k. fgrep / -F
The option ‘-F’ or fgrep is used to search for special character like ^ or { or ] or ‘’ or any other for that matter.
l. -E
The option ‘-E’ is also used to search special characters like the option’-F’ but you have to use ‘\’ as an escape character before the special character.
E.g: If you want to search : ‘car’ you can do so by:
grep -E ‘\’car\’’
Optimizing search results by Combining options used with Grep command:
Now that we have seen most of the tags that go with the grep command, let us see a few examples on how we can combine 2 or more options to narrow down or give a more desirable output.
Example 1:
grep -ino “lamborghini” cars.txt
Output:
The ‘-ino’ is a combination of ‘-i’ option which makes the grep case insensitive which gave the output as ‘Lamborghini’ even though I entered ‘lamborghini’, the ‘-n’ option gave me the line number the word was in, and the ‘-0’ option gave me only the word I wanted and not the entire sentence it is in.
Example 2:
grep -win -A 3 “James william” names.txt
Output:
1: James William2: James Williams
3: James Williamson
4: James Williamsons
The ‘-win – A 3’ is a combination of ‘-w’ option which I used so that I can forcibly choose only ‘James William’ and not the other names because the others have ‘James William’ as a subscript in them. The option ‘-i’ made the grep command case insensitive which gave the output as ‘James William’ even though I entered ‘james william’. The ‘-n’option printed out the line number of which the word is in, and the ‘-A 3’ option gave me 3 lines after the matching result.
Example 3:
Using Grep with pipe:
history | grep “git command”
Output:
This command will first run the history command, which will send all the output as the input to the grep command (that is what pspie is used for, it sends the output of the first command as the input to the second command). Hen grep will search for the files with the name “git command” and print them out.
Different options can be used together to narrow down your search or simply get better results. Here are 2 more search optimization techniques similar to options:
a. ^
The ‘^’ is used to display all lines that begin with the word you search for.
Syntax:
grep “^word/sentence”<file(s)>
Example:
grep “^T” cars.txt
Output:
b. $
The ‘$’ is used to display all lines that end with the word you search for.
Syntax:
grep “word/sentence$”<file(s)>
Example:
grep “ferrari$” cars.txt
Output:
If I write:
grep -in “ferrari$” cars.txt
It will print:
9: Test words: FeRrArI, feRRari, ferrARI
12: Lamborghini is better than ferrari
As ‘-i’ will make grep case insensitive, and ‘-n’ give the line numbers
Summary
As you have seen the grep command is a very powerful command which helps you to quickly find different patterns of characters in a file or files. The grep command can also be combined with many other linux commands like sort, pipe and a lot more, which make work flow a lot more efficient, especially when you are working on large projects with large numbers of files.
We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google