Difference between revisions of "Linux/C3/The-grep-command/English"
Line 14: | Line 14: | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 1 | | style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 1 | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''grep''' command | | style="border:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''grep''' command | ||
+ | |||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 2 | | style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 2 | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn | + | | style="border:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn |
+ | grep command. | ||
+ | We will do this with the help of an examples. | ||
|- | |- | ||
Line 25: | Line 28: | ||
'''Ubuntu Linux''' 12.04''' Operating System''' and | '''Ubuntu Linux''' 12.04''' Operating System''' and | ||
− | '''GNU BASH''' version 4.2.24 | + | '''GNU BASH''' version 4.2.24 |
Please note,''' GNU bash''' version 4 or above is recommended to practice this tutorial. | Please note,''' GNU bash''' version 4 or above is recommended to practice this tutorial. | ||
Line 31: | Line 34: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 4 | | style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 4 | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| As prerequisites |
You should know Basics of linux terminal | You should know Basics of linux terminal | ||
Line 52: | Line 55: | ||
To find a keyword in a paragraph or a line, we use grep command | To find a keyword in a paragraph or a line, we use grep command | ||
+ | Let us move on to grep. | ||
|- | |- | ||
Line 67: | Line 71: | ||
“'''grepdemo.txt'''<nowiki>” [</nowiki>enter] | “'''grepdemo.txt'''<nowiki>” [</nowiki>enter] | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| I will demonstrate usage of '''grep''' using a demo file '''grepdemo.txt''' |
Line 80: | Line 84: | ||
The fields are separated by a bar, which is called a '''delimiter'''. | The fields are separated by a bar, which is called a '''delimiter'''. | ||
+ | |||
|- | |- | ||
Line 97: | Line 102: | ||
− | Say we want to see who are the students in the '''computers''' stream. | + | 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. | ||
+ | So press '''CTRL + ALT and T''' keys simultaneously on your keyboard. | ||
− | + | Now type on the terminal: | |
− | '''grep '''space ( | + | '''grep '''space (within double quotes) '''computers''' space '''grepdemo.txt''' |
Line 108: | Line 115: | ||
− | Compare the result with the original file. | + | Now Compare the result with the original file. |
+ | Come back to our text editor. | ||
− | We see that the entry for''' Zubin''' is not enlisted. | + | We see that the entry for ''' Zubin''' is not enlisted. |
− | Why | + | Why this is so? |
This is because '''grep''' searched for the pattern '''“computers”''' with small '''c''' | This is because '''grep''' searched for the pattern '''“computers”''' with small '''c''' | ||
Line 129: | Line 137: | ||
| style="border:1pt solid #000000;padding:0.097cm;"| The pattern matching is case sensitive. | | style="border:1pt solid #000000;padding:0.097cm;"| The pattern matching is case sensitive. | ||
− | To make it case insensitive, we need to use the '''minus i '''option with '''grep'''. | + | To make it case insensitive, we need to use the '''minus i ''' option with '''grep'''. |
Type: | Type: | ||
− | '''grep''' space (minus''') i''' space ( | + | '''grep''' space (minus''') i''' space (within double quotes) “'''computers”''' after the double quotes space '''grepdemo.txt''' |
− | Press''' Enter.''' | + | Press ''' Enter.''' |
This will now enlist all the four entries. | This will now enlist all the four entries. | ||
+ | |||
|- | |- | ||
Line 172: | Line 181: | ||
− | Type: | + | For this Type: |
Line 194: | Line 203: | ||
'''press Enter''' | '''press Enter''' | ||
+ | The output is displayed. | ||
|- | |- | ||
Line 204: | Line 214: | ||
At the prompt type: | At the prompt type: | ||
− | '''grep''' space '''minus i''' space | + | '''grep''' space '''minus i''' space within double quotes '''fail''' after the double quotes space '''grepdemo.txt''' |
Line 224: | Line 234: | ||
We have the '''minus n''' option | We have the '''minus n''' option | ||
− | |||
− | |||
Revision as of 13:24, 14 December 2013
Title of script: The grep command and Regular Expressions
Author : Sachin Patil
Keywords:Search,Pattern,regular expressions,extended regular expression
|
|
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 an examples. |
Display Slide 3 | To record this tutorial, I am using
Ubuntu Linux 12.04 Operating System and GNU BASH version 4.2.24 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: |
First let us know about regular expressions.
Regular expressions are pattern matching techniques. When we have to find out whether a pattern exist 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. | |
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 grepdemo.txt
|
At the prompt type
grep “computers” grepdemo.txt [enter]
|
Let us see how grep works.
Now type on the terminal:
We see that the entry for Zubin is not enlisted.
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. Type: grep space (minus) i space (within double quotes) “computers” after the double quotes space grepdemo.txt
|
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.
|
At the prompt type
“grep -iv “pass” grepdemo.txt > notpass.txt” [enter]
|
We can also store this result in another file
|
At the prompt type
“cat notpass.txt”
|
To see the content of file type:
|
At the prompt type
“grep -i “fail” grepdemo.txt” [enter] |
Now
grep space minus i space within double quotes fail after the double quotes space grepdemo.txt
This will include those students who are failed but their result is incomplete. |
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,
|
At the prompt type
“grep -i “ankit saraf” grepdemo.txt” [enter] |
The patterns so far have been of single word.
|
At the prompt type
“grep -i “fail” grepdemo.txt notpass.txt” [enter] |
We can also find patterns in multiple files
|
At the prompt type
“grep -c “Fail” grepdemo.txt” [enter] |
Suppose we may only want to know the number of matches or count .
|
Display Slide 6
Summary |
In this tutorial we learnt,
To see the content of a file eg. cat filename
eg. grep “computers” grepdemo.txt
eg. grep -i “computers” grepdemo.txt
eg. grep -iv “pass” grepdemo.txt
eg. grep -in “fail” grepdemo.txt
eg. grep -iv “pass” grepdemo.txt > notpass.txt
eg. grep -c “Fail” grepdemo.txt |
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
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 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 |
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. |