Welcome to the spoken tutorial on grep command.
In this tutorial, we will learn grep command.
We will do this with the help of some examples.
To record this tutorial, I am using:
Linux Operating System and
GNU BASH version 4.2.24
Please note, GNU bash version 4 or above is recommended to practice this tutorial.
As prerequisites, you should know basics of Linux terminal.
For relevant tutorials, please visit our website which is as shown.
First, let us know about regular expressions .
* Regular expressions are pattern matching techniques.
* when we have to find out whether a pattern exists in a line, paragraph or a file.
For ex. If you want to search a phone number in the telephone directory
or to find a keyword in a paragraph or a line, we use grep command. Let us move on to grep.
grep searches for one or more patterns in one or more lines, paragraph or a file.
If file-name is not mentioned, grep searches for the patterns in the standard input.
If filename is missing, grep searches for the patterns in the standard input.
I will demonstrate usage of grep using a demo file called grepdemo.txt.
Let us see the content of the file.
This is a file that has 13 entries.
Each entry has 6 fields- roll number, name, stream, marks and stipend amount.
The fields are separated by a bar which is called a delimiter.
Let us see how grep works.
Say, we want to use 'grep' command to see who are the students in the computers stream.
For this, we have to open the terminal.
Now, type on the terminal:
grep space (within double quotes) computers after the double quotes space grepdemo .txt
Press Enter.
This would enlist those entries where stream is computers.
Now, compare the result with the original file.
Come back to our text editor.
We see that the entry for Zubin is not enlisted.
Why this is so?
This is because grep searched for the pattern “computers” with small c
while for Zubin, the stream is “Computers” with a capital C.
The pattern matching is case sensitive.
To make it case insensitive, we need to use the minus i option with grep.
Come back to our terminal. Now, type: grep space minus i space within double quotes computers after the double quotes space grepdemo.txt
Press Enter.This will now enlist all the four entries.
So we saw, grep enlists only those lines of files that match a given pattern.
We may do the reverse.
It is possible to make grep enlist only those lines that do not match the pattern.
For that, we have the minus v option.
Say, we want to enlist those entries of students who have not passed.
We can also store this result in another file.
For this, type: grep space minus iv space within double quotes pass after the double quotes space grepdemo.txt space greater than sign space notpass.txt
Press Enter.
To see the content of the file, type: cat space notpass.txt .
Press Enter.The output is displayed.
Now, at the prompt, type:
grep space minus i space within double quotes fail after the double quotes space grepdemo.txt
and press Enter.
This is different.
This will include those students who are failed but their result is incomplete.
If we want to see the line number in the file at which the enlisted entries are, we have the minus n option.
Let us clear the prompt.
Now, type: grep space -in space within double quote "fail" after the double quotes space grepdemo.txt
Press Enter.
The line number is displayed.
The patterns so far have been of single word.
We may have multi-word patterns as well.
But the entire pattern must be within quotes.
So, type: grep space minus i space within double quotes ankit space saraf after the double quotes space grepdemo.txt
Press Enter.
We see that Ankit Saraf's record is displayed.
We can also find patterns in multiple files.
For this, type: grep space minus i space within double quotes fail after double quotes space grepdemo.txt space notpass.txt
Press Enter.
The output is displayed.
With multiple files, grep will write the name of the file in which the entry was found. grepdemo.txt and notpass.txt.
These are the records from notpass.txt file and these are the records from grepdemo.txt file.
Suppose, we may only want to know the number of matches or count .
For that, we have the minus c option.
So, type: grep space minus c space within double quotes Fail with a capital F after the quotes space grepdemo.txt
Press Enter.
This will give us the count of number of lines matched.
This brings us to the end of this tutorial.
Let us summarize.
In this tutorial, we learnt:
To see the content of a file e.g. cat filename
To list the entries of a particular stream e.g. grep “computers” grepdemo.txt
To ignore cases e.g. grep -i “computers” grepdemo.txt
Lines that do not match the pattern e.g. grep -iv “pass” grepdemo.txt
To list the line numbers with the entries e.g. grep -in “fail” grepdemo.txt
To store the result in another file e.g. grep -iv “pass” grepdemo.txt notpass.txt and
To know the count e.g. grep -c “Fail” grepdemo.txt.
As an assignment,
explore some other commands like -E, + and ?.
Watch the video available at the link shown below.
It summarizes the Spoken Tutorial project.
If you do not have good bandwidth, you can download and watch it.
The Spoken Tutorial Project Team:
Conducts workshops using spoken tutorials.
Gives certificates to those who pass an online test.
For more details, please write tocontact@spoken-tutorial.org
Spoken Tutorial project is a part of the Talk to a Teacher project.
It is supported by the National Mission on Education through ICT, MHRD, Government of India.
More information on this mission is available at: http://spoken-tutorial.org MEICT-Intro
This is Ashwini Patil from IIT Bombay, signing off. Thank you for joining.