
All About GREP in Linux
GREP or Global Regular Expression Print is the most used command in Linux considering it is one of the most potent commands for system administrators and developers alike. The primary purpose of GREP is to search for specific information using search criteria.
Upon entering the search criteria, the expression pattern mentioned in the search is searched for in a specific file. The pattern lines are printed once all the matches for the searched term are found. GREP comes in handy when scrolling through large log files.
See below the Syntax and Description of Options used in the GREP command:
Syntax:
Grep [options] pattern [files] – l: it is used to find a list of filenames – v : It displays the lines which do not match the pattern searched for – c : It prints the lines that match the pattern searched for – h : It does not display filenames upon search. It only displays the lines that match the pattern searched for – i : It ignores match cases that are input -v : It prints out the lines that do not match the pattern searched for – n : It displays the line number along with the line that matches the pattern searched for – w : It displays the whole world that is matched – e exp : It can be used multiple times to specify an expression – E : It treats pattern as an extended regular expression – f file : It is used to search for patterns from a specific file; one per line – o : It displays the matched pattern line by line. Each matched pattern is displayed as a single line – A n : It displays the line that is searched and n lines after the search result – B n : It displays the line that is searched and n line before the search result – C n : It displays the line that is searched and n lines after and before the search result |
How to use GREP to search a file on Linux?
Here is a simple example of using GREP to search a file. It shows how to find a specific word in a file after eliminating similar words to get exact search results.
Step 1 – Search /etc/passwd file
Step 2 – For boo user, enter : grep boo/etc/passwd
Output for this command will be:
Foo:x:1000:1000:boo,,,:/home/boo:/bin/rad
Step 3 – Find all the lines containing a specific word.
For example:
To find addresses that contain the word “Singapore” in the address.txt file of the directory, you can run the command.
Fgrep Singapore address.txt
To make sure your search results do not contain other words with similar spelling as a prefix or suffix, you can run the command
Fgrep -w Singapore address.txt
Ignore word case matches using this command
Grep – i “boo”/etc/passwd
Here is another simple example where you can search for a phrase:
Let us consider this as the text to search:
“The mirage that is seen
It is not valid until you visit there
And find it still there.
You know who
Whom do you know
You search and find
Inner peace
It is a state of mind
And of heart.”
Task 1 – To find the lines that contain the word ‘mind’
Bash – grep mind Poetry.txt
Output
It is a state of mind
Although, the step is to look for the pattern ‘mind’, the GREP command searches through the file to find matches for the design.
type ‘grep’ -> pattern you are searching for -> file name in which you are conducting the search
Task 2 – Search a phrase in the file
When you search for a phrase in your file instead of searching for a word, you can use -n to do so. The lines containing the word “who: and -n” could be found easily via combined options.
Bash – grep -n -w “who” poetry.txt
Output:
5: You know who
6: Whom do you know
Task 3 – Make your search case-insensitive
To initiate the search on the file case-insensitive, you can use the option -i
Bash:
Grep -n -w -i “the” poetry.txt
Output:
1: The mirage that is seen
3: You reach the place
Task 4 – Eliminate lines that contain a particular word.
If you do not want to display the lines of a file that contain a specific word, you can use the option -v
Bash:
Grep -n -w -v “the” poetry.txt
Output:
2: It is not true, until
4: And find it still there.
5: You know who
6: Whom do you know
7: You search and find
8: Inner peace
9: It is a state of mind
10: And of heart
Task 5 – Search for a pattern in a set of files that are in sub-directories. Also called a ‘recursive’ search.
Example:
Search recursively for ‘Tomorrow’ in the ‘shell-lesson-data/practice’ directory:
Bash
Grep -r Tomorrow
Output
Data/saltrice.txt: “tomorrow, when you know the value of time, you will realize your mistake
Data/saltrice.txt: tomorrow is finally the day we have been waiting for
Data/saltrice.txt: tomorrow is unknown; today is a gift
What are the Other Options in GREP, and How to Find them?
There are a host of other options in GREP apart from the ones listed above. Below is the list consisting of the options that can be used on Linux OS by typing the commands:
Bash:
Grep –help
Output
Usage: grep [OPTION] . . . PATTERN [FILE] . . .
Search for PATTERN in each FILE
PATTERN is, by default, a basic regular expression (BRE)
Regexp Selection and Interpretation:
– e, – -regexp=PATTERN: This is to use PATTERN for matching – i, – – ignore-case : This is to ignore case distinctions – E, – – extended-regexp : PATTERN is an extended regular expression (ERE) – f, – – file=FILE : This is to obtain PATTERN from FILE – F, – – fixed-strings : PATTERN is a set of newline-separated fixed strings – G, – – basic-regexp : PATTERN is a basic regular expression (BRE) – w, – – word-regexp : force PATTERN to only match the whole words – x, – – line-regexp : force PATTERN to match complete lines only -z, – – null -data : a data line ends in 0 byte |
Conclusion
The above details provide a comprehensive list of options that can be used with GREP. The given details and examples should have provided you with the knowledge and understanding of the output of every option used. As a beginner, you can start with the basic features, such as finding a word, removing a comment, or getting complete results for the pattern you are searching for.
Once you have a firm grip on the basics, you can use other options to find lines, find phrases, eliminate words in terms, and do so much more. Linux is a thoroughly tested and free operating system that anyone can use. It is used by many enterprise infrastructures. GREP might take you some time to learn if you are new to it, but once you master it, you can do great things. Because enterprises rely on Linux, candidates with this skill-set are in high demand.