GREP is a command line search utility or tool to filter the input given to it. Grep got its name from ed editor as g/re/p (global / regular expression / print). Grep command can improve a command output by filtering out the required information. Grep will become a killer command when we combined it with regular expressions. In this post, we will see how to use grep command in a basic way and then move on some advanced and rarely used options. In our next couple of posts, we will see what grep can do with the help of regular expressions.
GREP command syntax
grep [options] [searchterm] filename
or
command | grep [options] [searchterm]
Before starting to grep command examples, I used below file which contains following content.
cat file1.txt
Output:
surendra 31 IBM Chennai Steve 45 BOA London Barbi 25 EasyCMDB Christchurch Max 25 Easy CMDB Christchurch Nathan 20 Wipro Newyark David 20 ai Newyark
Search single file using grep
Example 1: Search for a word “nathan” in file1.txt
grep nathan file1.txt
You don’t get any output, as the word nathan is not there. By this type you should know grep is a case sensitive command. If you want specifically Nathan, use caps N in nathan and try once again.
Example 2: Search for exact word “Nathan”
root@linuxnix:~# grep Nathan file1.txt Nathan 20 Wipro Newyark
Example 3: Search for a word which has either capital or small letters in it, no confusion between nathan or Nathan. The -i for ignoring case.
root@linuxnix:~# grep -i Nathan file1.txt Nathan 20 Wipro Newyark
Example 4: I suggest you always use single quotes for your search term. This will avoid confusions to gerp. Suppose if you want to search for “Easy CMDB” in a file, it will be difficult to search without single quotes. Try below examples.
Without quotes:
root@linuxnix:~# grep Easy CMDB file1.txt grep: CMDB: No such file or directory file1.txt:Barbi 25 EasyCMDB Christchurch file1.txt:Max 25 Easy CMDB Christchurch
What grep did?
If you observe, you got an error stating that there is no file called CMDB. That is true; there is no such file. This output has two issues
1) Grep is considering the second word in the command as file
2) Grep is thinking just “Easy” as a search term.
Example 5: Search for exact search term using single quotes.
root@linuxnix:~# grep 'Easy CMDB' file1.txt Max 25 Easy CMDB Christchurch
You may get doubt why single quotes and not double quotes. You can use double quotes as well when you want to send bash variable into search term.
Example 6: Search for a shell variable in a file. My shell is NAME1 which is assigned with Nathan. See below examples of single and double quotes.
root@linuxnix:~# NAME1=Nathan root@linuxnix:~# grep '$NAME1' file1.txt
No output, now try with double quotes
root@linuxnix:~# grep "$NAME1" file1.txt Nathan 20 Wipro Newyark root@linuxnix:~#
See the difference? So it depends on when you want to use single quotes and double quotes. If you want to pass shell variables to grep use double quotes and remaining time always use single quotes.
Example 7: If you want to inverse your search criteria, ie, to display all the lines which do not contain your search term then use -v option. The -v option will show all the lines which do not contain your search term. Suppose list all the lines which do not contain Nathan in the file.
root@linuxnix:~# grep -v 'Nathan' file1.txt surendra 31 IBM Chennai Steve 45 BOA London Barbi 25 EasyCMDB Christchurch Max 25 Easy CMDB Christchurch David 20 ai Newyark
Example 8: How about getting the count of a word in a file? Search for word ‘Easy’ and tell me how many times it is there in the file.
root@linuxnix:~# grep -c 'Easy' file1.txt 2
Example 9: Ok, finding some occurrences is okay, how about displaying line number as well for our search term? Search for Easy and display line number where that word is present.
root@linuxnix:~# grep -n 'Easy' file1.txt 3:Barbi 25 EasyCMDB Christchurch 4:Max 25 Easy CMDB Christchurch
Example 10: Some times you want to find exact word instead of finding an occurrence of a word sequence. Suppose you want to find is a word in a file, this will grep “this” as well as “this” contains “is” in it. Grep has a Technic to do this with the help of -w which filter out this type of stuff. Suppose I want to search for ai in our file, with standard grep it will print first and last lines as both contain ai in them. Use -w for avoiding this stuff.
With out -w option:
root@linuxnix:~# grep 'ai' file1.txt surendra 31 IBM Chennai David 20 ai Newyark
With -w option:
root@linuxnix:~# grep -w 'ai' file1.txt David 20 ai Newyark
Example 11: Grep has a unique feature which can print lines before, after and between for a search term. Suppose if you want to print two lines before to the search, use below example. Search for Max and below example prints two lines above search term as well. This will be handy when searching for log files.
root@linuxnix:~# grep -B 2 'Max' file1.txt
Steve 45 BOA London
Barbi 25 EasyCMDB Christchurch
Max 25 Easy CMDB Christchurch
Example 12: Search for a word and print two lines after to this search
root@linuxnix:~# grep -A 2 'Barbi' file1.txt
Barbi 25 EasyCMDB Christchurch
Max 25 Easy CMDB Christchurch
Nathan 20 Wipro Newyark
Example 13: Search for a word and print one line Before and after center to this search term
root@linuxnix:~# grep -C 1 'Max' file1.txt
Barbi 25 EasyCMDB Christchurch
Max 25 Easy CMDB Christchurch
Nathan 20 Wipro Newyark
Search multiple files using grep
Example 14: Grep has one more feature which allows searching multiple files at a time. We no need to use grep for each file. Suppose you want to search for Nathan in /root folder recursively use below example.
root@linuxnix:~# grep -r 'Nathan' /root/ Nathan 20 Wipro Newyark
This will not display which file contain Nathan who may confuse you. We do not know from which file Nathan came. To avoid this confusion use below example.
Example 15: Search for a word and just list the file names which contain it with in a directory.
root@linuxnix:~# grep -l 'Nathan' /root/ file1.txt
Example 16: Ok, again this command do not show the line which is again a confusion. To search multiple files and display their contents which match our search criteria use combination of -r and -n
root@linuxnix:~# grep -rn 'Nathan' /root/ file1.txt:5:Nathan 20 Wipro Newyark test.txt:1:Nathan abc xyz who
Example 17: Restrict your search to specific files. Suppose search only txt files.
root@linuxnix:~# grep -rn 'Nathan' *.txt file1.txt:5:Nathan 20 Wipro Newyark test.txt:1:Nathan abc xyz who
Example 18: Some times you may see below error when opening log files.
Binary file <input-file> matches.
This error is because grep thinks it is a binary file. But our log files are never a binary one. This error is due to the null value found in the log file. To read such files, you have to use -a which tell grep to read this file as a binary file.
grep -a xyz abc.txt
In our next two posts, we will see how to use grep with regular expressions.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018