Difference between revisions of "Linux/C3/The-grep-command/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(3 intermediate revisions by one other user not shown)
Line 1: Line 1:
'''Title of script''': The grep command and Regular Expressions
+
'''Title of script''': The grep command
  
'''Author : Sachin Patil'''
+
'''Author : Sachin Patil and Anirban'''
  
 
'''Keywords:Search,Pattern,regular expressions,extended regular expression'''
 
'''Keywords:Search,Pattern,regular expressions,extended regular expression'''
Line 7: Line 7:
  
  
{| style="border-spacing:0;"
+
{|border=1
| style="border:1pt solid #000000;padding:0.097cm;"| <center>'''Visual Cue'''</center>
+
!'''Visual Cue'''  
| style="border:1pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center>
+
!'''Narration'''  
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 1
+
|Display Slide 1
| style="border:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''grep''' command
+
|Welcome to the spoken tutorial on '''grep''' command
 
+
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 2
+
|Display Slide 2
| style="border:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn
+
|In this tutorial we will learn '''grep command'''.
grep command.
+
We will do this with the help of some examples.
We will do this with the help of an examples.
+
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 3
+
|Display Slide 3
| style="border:1pt solid #000000;padding:0.097cm;"| To record this tutorial, I am using
+
|To record this tutorial, I am using
  
'''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.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 4
+
|Display Slide 4
| style="border:1pt solid #000000;padding:0.097cm;"| As prerequisites
+
|As prerequisites
 
+
You should know Basics of linux terminal
+
  
For relevant tutorials please visit our website:
+
You should know basics of '''Linux terminal'''
  
http://spoken-tutorial.org
+
For relevant tutorials, please visit our website which is as shown.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
|
| style="border:1pt solid #000000;padding:0.097cm;"| First let us know about regular expressions.
+
|First let us know about regular expressions.
  
Regular expressions are pattern matching techniques.
+
*Regular expressions are pattern matching techniques.
  
When we have to find out whether a pattern exist in a line, paragraph or a file.
+
*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  
 
For ex. If you want to search a phone number in the telephone directory  
Line 54: Line 49:
 
Or  
 
Or  
  
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.
+
Let us move on to '''grep'''.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 5
+
|Display Slide 5
| style="border:1pt solid #000000;padding:0.097cm;"| '''grep''' searches for one or more patterns in one or more line, paragraph or a file.
+
|'''grep''' searches for one or more patterns in one or more line, paragraph or a file.
  
  
If filename is not mentioned''' grep''' search for the patterns in the standard input.
+
If filename is not mentioned,''' grep''' search for the patterns in the standard input.
  
  
Line 68: Line 63:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Open
+
|Open
  
 
“'''grepdemo.txt'''<nowiki>” [</nowiki>enter]
 
“'''grepdemo.txt'''<nowiki>” [</nowiki>enter]
| style="border:1pt solid #000000;padding:0.097cm;"| I will demonstrate usage of '''grep''' using a demo file '''grepdemo.txt'''
+
|I will demonstrate usage of '''grep''' using a demo file '''grepdemo.txt'''
  
  
Let us see the content of the file
+
Let us see the content of the file.
  
 
+
|-
This is a file that has 13 entries.  
+
|
 +
|This is a file that has 13 entries.  
  
  
 
Each entry has 6 fields roll, name, stream, marks, and stipend amount.  
 
Each entry has 6 fields roll, name, stream, marks, and stipend amount.  
  
 
+
|-
The fields are separated by a bar, which is called a '''delimiter'''.
+
|
 
+
|The fields are separated by a bar, which is called a '''delimiter'''.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| At the prompt type
+
|At the prompt type
  
 
'''grep “computers” grepdemo.txt'''<nowiki> [</nowiki>enter]
 
'''grep “computers” grepdemo.txt'''<nowiki> [</nowiki>enter]
Line 96: Line 92:
  
 
'''cat grepdemo.txt'''
 
'''cat grepdemo.txt'''
 +
|Let us see how '''grep''' works.
  
  
 +
Say we want to use '''grep command''', to see who are the students in the '''computers''' stream.
  
| style="border:1pt solid #000000;padding:0.097cm;"| Let us see how grep works.
+
|-
 
+
|
 +
|For this we have to open the '''terminal'''.
  
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.
 
So press '''CTRL + ALT and T''' keys simultaneously on your keyboard.
  
Now type on the terminal:
+
|-
 +
|
 +
|Now type on the '''terminal''':
  
  
'''grep '''space (within double quotes) '''computers''' space '''grepdemo.txt'''
+
'''grep space (within double quotes) computers space grepdemo .txt'''
  
 +
|-
 +
|
 +
|This would enlist those entries where stream is '''computers'''.
  
This would enlist those entries where stream is '''computers'''
+
|-
 +
|
 +
|Now compare the result with the original file.
  
 
+
|-
Now Compare the result with the original file.
+
|
Come back to our text editor.
+
|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 this is so?
+
|Highlight
 +
|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'''  
  
  
While for '''Zubin''' the stream is “Computers” with a capital '''C'''.
+
While for '''Zubin''', the stream is “Computers” with a capital '''C'''.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| At the prompt type
+
|At the prompt type
  
 
“'''grep -i “computers” grepdemo.txt'''<nowiki>” [</nowiki>enter]
 
“'''grep -i “computers” grepdemo.txt'''<nowiki>” [</nowiki>enter]
  
 +
|The pattern matching is '''case sensitive'''.
  
 
+
To make it '''case''' insensitive, we need to use the '''minus i ''' option with '''grep'''.
| 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'''.
+
  
 
Type:
 
Type:
  
'''grep''' space (minus''') i''' space (within double quotes) “'''computers”''' after the double quotes space '''grepdemo.txt'''
+
'''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.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| At the prompt type
+
|At the prompt type
  
 
“'''grep -iv “pass” grepdemo.txt'''<nowiki>” [</nowiki>enter]
 
“'''grep -iv “pass” grepdemo.txt'''<nowiki>” [</nowiki>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.
  
| style="border:1pt solid #000000;padding:0.097cm;"| So we saw, '''grep''' enlists only those lines of files that match a given pattern.
+
|-
 +
|
 +
|For that we have the '''minus v''' option.
  
 
+
|-
We may do the reverse.
+
|
 
+
|Say, we want to enlist those entries of students who have not passed.
 
+
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.
+
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| At the prompt type
+
|At the prompt type
  
 
“'''grep -iv “pass” grepdemo.txt > notpass.txt'''<nowiki>” [</nowiki>enter]
 
“'''grep -iv “pass” grepdemo.txt > notpass.txt'''<nowiki>” [</nowiki>enter]
  
  
 +
|We can also store this result in another file
  
| style="border:1pt solid #000000;padding:0.097cm;"| We can also store this result in another file
 
 
 
For this Type:
 
  
 +
For this type:
  
'''grep''' space '''minus iv''' space in double quotes '''pass''' space '''grepdemo.txt''' space greater than sign space '''notpass.txt '''
 
  
 +
'''grep space minus iv space''' in double quotes '''pass space grepdemo.txt space greater than sign space notpass.txt '''
  
'''Press Enter.'''
+
Press '''Enter.'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| At the prompt type
+
|At the prompt type
  
 
'''“cat notpass.txt”'''
 
'''“cat notpass.txt”'''
 
+
|To see the content of file, type:
 
+
 
+
| style="border:1pt solid #000000;padding:0.097cm;"| To see the content of file type:
+
  
  
 
'''cat space notpass.txt '''
 
'''cat space notpass.txt '''
  
 +
Press '''Enter'''.
  
'''press Enter'''
 
 
The output is displayed.
 
The output is displayed.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| At the prompt type
+
|At the prompt type
  
 
“'''grep -i “fail” grepdemo.txt'''<nowiki>” [</nowiki>enter]
 
“'''grep -i “fail” grepdemo.txt'''<nowiki>” [</nowiki>enter]
| style="border:1pt solid #000000;padding:0.097cm;"| Now
+
|Now at the '''prompt''' type:
  
 +
'''grep space minus i space''' within double quotes '''fail''' after the double quotes space '''grepdemo.txt'''
  
At the prompt type:
+
Press '''Enter.'''
 
+
'''grep''' space '''minus i''' space within double quotes '''fail''' after the double quotes space '''grepdemo.txt'''
+
 
+
 
+
'''Press Enter.'''
+
 
+
 
+
This is different.
+
  
 +
|-
 +
|
 +
|This is different.
 
    
 
    
  
Line 227: Line 219:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| At the prompt type
+
|At the prompt type
  
 
“'''grep -in “fail” grepdemo.txt'''<nowiki>” [</nowiki>enter]
 
“'''grep -in “fail” grepdemo.txt'''<nowiki>” [</nowiki>enter]
| style="border:1pt solid #000000;padding:0.097cm;"| If we want to see the line number in the file at which the enlisted entries are,  
+
|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
  
We have the '''minus n''' option
+
"'''grep space -in space within double quote "fail" after the double quotes space grepdemo.txt'''
  
 +
Press '''Enter'''.
 +
 +
The line number is displayed.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| At the prompt type
+
|At the prompt type
  
 
“'''grep -i “ankit saraf” grepdemo.txt'''<nowiki>” [</nowiki>enter]
 
“'''grep -i “ankit saraf” grepdemo.txt'''<nowiki>” [</nowiki>enter]
| style="border:1pt solid #000000;padding:0.097cm;"| The patterns so far have been of single word.
+
|The patterns so far have been of single word.
  
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 multiword patterns as well.  
 
We may have multiword patterns as well.  
  
  
But the entire pattern must be within quotes.
+
But the entire pattern must be within '''quotes'''.
  
 +
|-
 +
|
 +
|So type:
  
So Type:
 
  
 +
'''grep space minus i space''' in double quotes '''ankit space saraf space grepdemo.txt '''
  
'''grep''' space '''minus i''' space in double quotes '''ankit''' space '''saraf''' space '''grepdemo.txt '''
 
  
 +
Press '''Enter'''.
  
'''Press Enter'''
+
We see that '''Ankit Saraf''''s record is displayed.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| At the prompt type
+
|At the prompt type
  
 
“'''grep -i “fail” grepdemo.txt notpass.txt'''<nowiki>” [</nowiki>enter]
 
“'''grep -i “fail” grepdemo.txt notpass.txt'''<nowiki>” [</nowiki>enter]
| style="border:1pt solid #000000;padding:0.097cm;"| We can also find patterns in multiple files
+
|We can also find patterns in multiple files.
  
  
Line 272: Line 267:
  
  
'''grep '''space '''minus i''' space in double quotes '''fail''' space '''grepdemo.txt''' space '''notpass.txt'''  
+
'''grep space minus i space''' in double quotes '''fail space grepdemo.txt space notpass.txt'''  
  
  
'''Press Enter'''
+
'''Press Enter'''.
  
 +
The output is displayed.
  
With multiple files '''grep''' will write the name of the file in which the entry was found.
+
|-
 +
|
 +
|With multiple files, '''grep''' will write the name of the file in which the entry was found.
 +
 
 +
'''grepdemo.txt''' and '''notpass.txt'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| At the prompt type
+
|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”''' <nowiki>[</nowiki>enter]
 
“'''grep -c “Fail” grepdemo.txt”''' <nowiki>[</nowiki>enter]
| style="border:1pt solid #000000;padding:0.097cm;"| Suppose we may only want to know the '''number of matches '''or '''count''' .
+
|Suppose we may only want to know the '''number of matches '''or '''count''' .
  
 
+
For that, we have the '''minus c''' option
For that we have the '''minus c''' option
+
  
  
 
Type:
 
Type:
  
 +
'''grep space minus c space''' in double quotes '''Fail  with a capital F space grepdemo.txt'''
  
'''grep''' space '''minus c''' space in double quotes '''Fail''' space '''grepdemo.txt'''
+
Press '''Enter'''.
  
  
'''Press Enter'''
+
This will give us the '''count''' of number of lines matched.
  
 +
|-
 +
|
 +
|This brings us to the end of this tutorial.
  
This will give us the '''count''' of number of lines matched.
+
Let us summarize.
  
 
|-
 
|-
Line 307: Line 314:
 
| style="border:1pt solid #000000;padding:0.176cm;"| In this tutorial we learnt,
 
| style="border:1pt solid #000000;padding:0.176cm;"| In this tutorial we learnt,
  
To see the content of a file
+
*To see the content of a file
 
+
**eg. '''cat filename'''
eg. '''cat filename'''
+
*To list the entries of a particular stream
 
+
**eg. '''grep “computers” grepdemo.txt'''
 
+
*To ignore cases
To list the entries of a particular stream
+
**eg. '''grep -i “computers” grepdemo.txt'''
 
+
*Lines that do not match the pattern
eg. '''grep “computers” grepdemo.txt'''
+
**eg. '''grep -iv “pass” grepdemo.txt'''
 
+
*To list the line numbers with the entries
 
+
**eg.''' grep -in “fail” grepdemo.txt'''
To ignore cases
+
*To store the result in another file
 
+
**eg. '''grep -iv “pass” grepdemo.txt > notpass.txt'''
eg. '''grep -i “computers” grepdemo.txt'''
+
*To know the count
 
+
**eg.''' grep -c “Fail” grepdemo.txt'''
 
+
Lines that do not match the pattern
+
 
+
eg. '''grep -iv “pass” grepdemo.txt'''
+
 
+
 
+
To list the line numbers with the entries
+
 
+
eg.''' grep -in “fail” grepdemo.txt'''
+
 
+
 
+
To store the result in another file
+
 
+
eg. '''grep -iv “pass” grepdemo.txt > notpass.txt'''
+
 
+
 
+
To know the count
+
 
+
eg.''' grep -c “Fail” grepdemo.txt'''
+
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 7
+
|Display Slide 7
| style="border:1pt solid #000000;padding:0.097cm;"| As an assignment,
+
|As an assignment,
  
 
Explore some other commands like '''-E, + and ?'''
 
Explore some other commands like '''-E, + and ?'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 8
+
|Display Slide 8
  
 
Acknowledgement Slide
 
Acknowledgement Slide
| style="border:1pt solid #000000;padding:0.097cm;"| Watch the video available at the link shown below
+
|Watch the video available at the link shown below.
  
It summarises the Spoken Tutorial project
+
It summarises the Spoken Tutorial project.
  
If you do not have good bandwidth, you can download and watch it
+
If you do not have good bandwidth, you can download and watch it.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 9
+
|Display Slide 9
  
 
Spoken Tutorial Workshops
 
Spoken Tutorial Workshops
| style="border:1pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team
+
|The Spoken Tutorial Project Team
  
Conducts workshops using spoken tutorials
+
*Conducts workshops using spoken tutorials
 
+
*Gives certificates to those who pass an online test
Gives certificates to those who pass an online test
+
  
 
For more details, please write to
 
For more details, please write to
  
contact@spoken-tutorial.org
+
'''contact@spoken-tutorial.org'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Display Slide 10
+
|Display Slide 10
  
 
Acknowledgement
 
Acknowledgement
| style="border:1pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher project
+
|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
 
It is supported by the National Mission on Education through ICT, MHRD, Government of India
Line 382: Line 369:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| No Last Slide for tutorials created at IITB
+
|
 
+
|This is Ashwini Patil from IIT Bombay signning off. Thank you for joining.
Display the previous slide only and narrate this line.
+
| style="border:1pt solid #000000;padding:0.097cm;"| The script has been contributed by Sachin Patil.
+
 
+
This is Ashwini Patil from IIT Bombay signning off. Thank you for joining.
+
  
 
|}
 
|}

Latest revision as of 23:39, 14 December 2013

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
  • 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 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 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.


If filename is not mentioned, grep search for the patterns in the standard input.


If filename is missing, grep searches for the patterns in the standard input.

Open

grepdemo.txt” [enter]

I will demonstrate usage of grep using a demo file grepdemo.txt


Let us see the content of the file.

This is a file that has 13 entries.


Each entry has 6 fields roll, name, stream, marks, and stipend amount.

The fields are separated by a bar, which is called a delimiter.
At the prompt type

grep “computers” grepdemo.txt [enter]


At the prompt, type:


cat grepdemo.txt

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.

So press CTRL + ALT and T keys simultaneously on your keyboard.

Now type on the terminal:


grep space (within double quotes) computers space grepdemo .txt

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


While for Zubin, the stream is “Computers” with a capital 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

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


For this type:


grep space minus iv space in double quotes pass space grepdemo.txt space greater than sign space notpass.txt

Press Enter.

At the prompt type

“cat notpass.txt”

To see the content of file, type:


cat space notpass.txt

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

Press Enter.

This is different.


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, 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.


But the entire pattern must be within quotes.

So type:


grep space minus i space in double quotes ankit space saraf space grepdemo.txt


Press Enter.

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.


Type:


grep space minus i space in double quotes fail 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

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


Type:

grep space minus c space in double quotes Fail with a capital F 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.

Display Slide 6

Summary

In this tutorial we learnt,
  • To see the content of a file
    • eg. cat filename
  • To list the entries of a particular stream
    • eg. grep “computers” grepdemo.txt
  • To ignore cases
    • eg. grep -i “computers” grepdemo.txt
  • Lines that do not match the pattern
    • eg. grep -iv “pass” grepdemo.txt
  • To list the line numbers with the entries
    • eg. grep -in “fail” grepdemo.txt
  • To store the result in another file
    • eg. grep -iv “pass” grepdemo.txt > notpass.txt
  • To know the count
    • 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

This is Ashwini Patil from IIT Bombay signning off. Thank you for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey