BOSS-Linux/C3/The-grep-command/English
Title of script: The grep command
Author : Sachin Patil and Anirban
Keywords:Search,Pattern,regular expressions,extended regular expression
Visual Cue | Narration |
---|---|
Display Slide 1 | Welcome to the spoken tutorial on grep command |
Display Slide 2 | In this tutorial we will learn grep command.
We will do this with the help of some examples. |
Display Slide 3 | To record this tutorial, I am using
Please note, GNU bash version 4 or above is recommended to practice this tutorial. |
Display Slide 4 | 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.
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. | |
Display Slide 5 | grep searches for one or more patterns in one or more line, paragraph or a file.
|
Open
“grepdemo.txt” [enter] |
I will demonstrate usage of grep using a demo file called grepdemo.txt
|
This is a file that has 13 entries.
| |
The fields are separated by a bar, which is called a delimiter. | |
At the prompt type
grep “computers” grepdemo.txt [enter]
|
Let us see how grep works.
|
For this we have to open the terminal.
So press CTRL + ALT and T keys simultaneously on your keyboard. | |
Now type on the terminal:
| |
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. | |
Highlight | Why this is so?
This is because grep searched for the pattern “computers” with small c
|
At the prompt type
“grep -i “computers” grepdemo.txt” [enter] |
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 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. | |
At the prompt type
“grep -iv “pass” grepdemo.txt” [enter] |
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. | |
At the prompt type
“grep -iv “pass” grepdemo.txt > notpass.txt” [enter]
|
We can also store this result in another file
Press Enter. |
At the prompt type
“cat notpass.txt” |
To see the content of file, type:
Press Enter. The output is displayed. |
At the prompt type
“grep -i “fail” grepdemo.txt” [enter] |
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.
| |
At the prompt type
“grep -in “fail” grepdemo.txt” [enter] |
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. | |
At the prompt type
“grep -i “ankit saraf” grepdemo.txt” [enter] |
The patterns so far have been of single word.
We may have multiword patterns as well.
|
So type:
We see that Ankit Saraf's record is displayed. | |
At the prompt type
“grep -i “fail” grepdemo.txt notpass.txt” [enter] |
We can also find patterns in multiple files.For this
The output is displayed. |
With multiple files, grep will write the name of the file in which the entry was found.
grepdemo.txt notpass.txt | |
Highlight the relevant records. | These are the records from notpass.txt file and these are the records from grepdemo.txt file. |
At the prompt type
“grep -c “Fail” grepdemo.txt” [enter] |
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 space grepdemo.txt Press Enter.
|
This brings us to the end of this tutorial.
Let us summarize. | |
Display Slide 6
Summary |
In this tutorial we learnt,
|
Display Slide 7 | As an assignment,
Explore some other commands like -E, + and ? |
Display Slide 8
Acknowledgement Slide |
Watch the video available at the link shown below.
It summarises the Spoken Tutorial project. If you do not have good bandwidth, you can download and watch it. |
Display Slide 9
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
For more details, please write to contact@spoken-tutorial.org |
Display Slide 10
Acknowledgement |
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\NMEICT-Intro |
This is Ashwini Patil from IIT Bombay signning off. Thank you for joining. |