Linux/C3/More-on-grep-command/English
Title of script: The grep command and Regular Expressions
Author : Sachin Patil
Keywords:Search,Pattern,regular expressions,extended regular expression
Visual Cue | Narration |
Display Slide 1
|
Welcome to this spoken tutorial on some more on grep command |
Display Slide 2 | In this tutorial we will learn some more grep commands. |
Display Slide 3 | To record this tutorial, I am using
Ubuntu Linux 12.04 Operating System and GNU BASH version 4.2.24(1) Please note, GNU bash version 4 or above is recommended to practice this tutorial. |
Display Slide 4 | To practice this tutorial,
You should know Basics of linux terminal. You should be aware of grep. For relevant tutorials please visit our website: |
At the prompt type
“grep -e “electronics” -e “civil” grepdemo.txt” [enter] |
We can match more than one patterns as well.
Then we have to use the -e option.
grep space minus e space in double quotes electronics space minus e space in double quotes civil space grepdemo.txt Press Enter. |
At the prompt type
“grep -ie “choudhury” -ie “chowdhari” grepdemo.txt” [enter] |
Say you need to look into people whose title is choudhury.
So what is the solution?.
Press Enter.
|
Display Slide 5 | A regular expression provides a concise and flexible means for matching strings of text.
|
There are a number of regular expression characters.
Let us see them one by one. | |
Display Slide 6 | The character class.
It allows us to specify a group of characters within a pair of square brackets .
Eg. [abc] would mean that this regular expression matches either a or b or c. |
At the prompt
grep -i “ch[ao][uw]dh[ua]r[yi]” grepdemo.txt [enter] |
Like to match choudhury we may use the command
type at the prompt
It still does not match choudhuree |
Display Slide 7 | If we want to specify a large range then write:
first letter dash last letter of the range
|
Display Slide 8
|
The * refers to 0 or more occurrences of the immediately preceding character.
|
At the prompt
grep -i “m[ei]*raa*” grepdemo.txt
|
So to match the students name whose name is Mira
We write at the prompt:
|
Display Slide 9 | The dot “.”
This regular expression matches any one character. |
At the prompt
grep “M...” grepdemo.txt
|
Say we want to search any words that are 4 letters long and start with M.
Press Enter.
|
Display Slide 10 | We may be specific about where in the line we want to search for our pattern.
|
At the prompt
|
Now if we want those entries whose roll numbers start with A.
We know that roll is the first field in the file. We write at the prompt:
|
Display Slide 11
grep “[78]...$ ” grepdemo.txt
|
Similarly to match a pattern at the end of the file, we have the dollar sign.
grep space in double quotes [78]...dolar sign space grepdemo.txt Press Enter
|
Display Slide 12
Summary |
In this tutorial we learnt,
eg. grep -e “electronics” -e “civil” grepdemo.txt
eg. grep -ie “choudhury” -ie “chowdhari” grepdemo.txt
eg. grep -i “ch[ao][uw]dh[ua]r[yi]” grepdemo.txt
eg. grep -i “m[ei]*raa*” grepdemo.txt
eg. grep “M...” grepdemo.txt
eg. grep “^A” grepdemo.txt
eg. grep “[78]...$ ” grepdemo.txt
|
Display Slide 13 | As an assignment,
List those entries that are 5 letters long and starts with Y. |
Display Slide 14
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 15
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to contact@spoken-tutorial.org |
Display Slide 16
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 |
No Last Slide for tutorials created at IITB
Display the previous slide only and narrate this line. |
The script has been contributed by Sachin Patil.
This is Ashwini Patil from IIT Bombay signning off. Thank you for joining. |