<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=BOSS-Linux%2FC3%2FThe-grep-command%2FEnglish</id>
		<title>BOSS-Linux/C3/The-grep-command/English - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=BOSS-Linux%2FC3%2FThe-grep-command%2FEnglish"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=BOSS-Linux/C3/The-grep-command/English&amp;action=history"/>
		<updated>2026-04-19T06:53:27Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=BOSS-Linux/C3/The-grep-command/English&amp;diff=19749&amp;oldid=prev</id>
		<title>PoojaMoolya: Created page with &quot;'''Title of script''': The grep command  '''Author : Sachin Patil and Anirban'''  '''Keywords:Search,Pattern,regular expressions,extended regular expression'''    {|border=1 !...&quot;</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=BOSS-Linux/C3/The-grep-command/English&amp;diff=19749&amp;oldid=prev"/>
				<updated>2015-01-29T07:00:31Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;#039;&amp;#039;&amp;#039;Title of script&amp;#039;&amp;#039;&amp;#039;: The grep command  &amp;#039;&amp;#039;&amp;#039;Author : Sachin Patil and Anirban&amp;#039;&amp;#039;&amp;#039;  &amp;#039;&amp;#039;&amp;#039;Keywords:Search,Pattern,regular expressions,extended regular expression&amp;#039;&amp;#039;&amp;#039;    {|border=1 !...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;'''Title of script''': The grep command&lt;br /&gt;
&lt;br /&gt;
'''Author : Sachin Patil and Anirban'''&lt;br /&gt;
&lt;br /&gt;
'''Keywords:Search,Pattern,regular expressions,extended regular expression'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{|border=1&lt;br /&gt;
!'''Visual Cue''' &lt;br /&gt;
!'''Narration''' &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|Display Slide 1&lt;br /&gt;
|Welcome to the spoken tutorial on '''grep''' command&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|Display Slide 2&lt;br /&gt;
|In this tutorial we will learn '''grep command'''.&lt;br /&gt;
We will do this with the help of some examples.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|Display Slide 3&lt;br /&gt;
|To record this tutorial, I am using&lt;br /&gt;
&lt;br /&gt;
*'''Linux'''  '''Operating System''' and&lt;br /&gt;
*'''GNU BASH''' version 4.2.24&lt;br /&gt;
&lt;br /&gt;
Please note,''' GNU bash''' version 4 or above is recommended to practice this tutorial.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|Display Slide 4&lt;br /&gt;
|As prerequisites&lt;br /&gt;
&lt;br /&gt;
You should know basics of '''Linux terminal'''&lt;br /&gt;
&lt;br /&gt;
For relevant tutorials, please visit our website which is as shown.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|First let us know about regular expressions.&lt;br /&gt;
&lt;br /&gt;
*Regular expressions are pattern matching techniques.&lt;br /&gt;
&lt;br /&gt;
*When we have to find out whether a pattern exist in a line, paragraph or a file.&lt;br /&gt;
&lt;br /&gt;
For ex. If you want to search a phone number in the telephone directory &lt;br /&gt;
&lt;br /&gt;
Or &lt;br /&gt;
&lt;br /&gt;
To find a keyword in a paragraph or a line, we use '''grep command'''.&lt;br /&gt;
Let us move on to '''grep'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|Display Slide 5&lt;br /&gt;
|'''grep''' searches for one or more patterns in one or more line, paragraph or a file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If filename is not mentioned,''' grep''' search for the patterns in the standard input.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If filename is missing, '''grep''' searches for the patterns in the standard input.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|Open&lt;br /&gt;
&lt;br /&gt;
“'''grepdemo.txt'''&amp;lt;nowiki&amp;gt;” [&amp;lt;/nowiki&amp;gt;enter]&lt;br /&gt;
|I will demonstrate usage of '''grep''' using a demo file called '''grepdemo.txt'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Let us see the content of the file.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|This is a file that has 13 entries. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Each entry has 6 fields roll number, name, stream, marks, and stipend amount. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|The fields are separated by a bar, which is called a '''delimiter'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|At the prompt type&lt;br /&gt;
&lt;br /&gt;
'''grep “computers” grepdemo.txt'''&amp;lt;nowiki&amp;gt; [&amp;lt;/nowiki&amp;gt;enter]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
At the prompt, type:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''cat grepdemo.txt'''&lt;br /&gt;
|Let us see how '''grep''' works.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Say we want to use '''grep command''', to see who are the students in the '''computers''' stream.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|For this we have to open the '''terminal'''.&lt;br /&gt;
&lt;br /&gt;
So press '''CTRL + ALT and T''' keys simultaneously on your keyboard.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|Now type on the '''terminal''':&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''grep space (within double quotes) computers (after the double quotes) space  grepdemo .txt'''.Press Enter&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|This would enlist those entries where stream is '''computers'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|Now compare the result with the original file. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|Come back to our text editor.&lt;br /&gt;
&lt;br /&gt;
We see that the entry for ''' Zubin''' is not enlisted.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|Highlight&lt;br /&gt;
|Why this is so?&lt;br /&gt;
&lt;br /&gt;
This is because '''grep''' searched for the pattern '''“computers”''' with small '''c''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
While for '''Zubin''', the stream is “Computers” with a capital '''C'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|At the prompt type&lt;br /&gt;
&lt;br /&gt;
“'''grep -i “computers” grepdemo.txt'''&amp;lt;nowiki&amp;gt;” [&amp;lt;/nowiki&amp;gt;enter]&lt;br /&gt;
&lt;br /&gt;
|The pattern matching is '''case sensitive'''.&lt;br /&gt;
&lt;br /&gt;
To make it '''case''' insensitive, we need to use the '''minus i ''' option with '''grep'''.Come back to our terminal&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
'''grep space (minus) i  space (within double quotes) “'''computers”''' after the '''double quotes space grepdemo.txt'''&lt;br /&gt;
&lt;br /&gt;
Press ''' Enter.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|This will now enlist all the four entries.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|At the prompt type&lt;br /&gt;
&lt;br /&gt;
“'''grep -iv “pass” grepdemo.txt'''&amp;lt;nowiki&amp;gt;” [&amp;lt;/nowiki&amp;gt;enter]&lt;br /&gt;
|So we saw, '''grep''' enlists only those lines of files that match a given pattern.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|We may do the reverse.&lt;br /&gt;
&lt;br /&gt;
It is possible to make '''grep''' enlist only those lines that '''do not''' match the pattern.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|For that we have the '''minus v''' option.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|Say, we want to enlist those entries of students who have not passed.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|At the prompt type&lt;br /&gt;
&lt;br /&gt;
“'''grep -iv “pass” grepdemo.txt &amp;gt; notpass.txt'''&amp;lt;nowiki&amp;gt;” [&amp;lt;/nowiki&amp;gt;enter]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|We can also store this result in another file&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For this type:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''grep space minus iv space''' within double quotes '''pass after the double quotes space grepdemo.txt space greater than sign space notpass.txt '''&lt;br /&gt;
&lt;br /&gt;
Press '''Enter.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|At the prompt type&lt;br /&gt;
&lt;br /&gt;
'''“cat notpass.txt”'''&lt;br /&gt;
|To see the content of file, type:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''cat space notpass.txt '''&lt;br /&gt;
&lt;br /&gt;
Press '''Enter'''.&lt;br /&gt;
&lt;br /&gt;
The output is displayed.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|At the prompt type&lt;br /&gt;
&lt;br /&gt;
“'''grep -i “fail” grepdemo.txt'''&amp;lt;nowiki&amp;gt;” [&amp;lt;/nowiki&amp;gt;enter]&lt;br /&gt;
|Now at the '''prompt''' type:&lt;br /&gt;
&lt;br /&gt;
'''grep space minus i space''' within double quotes '''fail''' after the double quotes space '''grepdemo.txt'''&lt;br /&gt;
&lt;br /&gt;
and Press '''Enter.'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|This is different.&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
This will include those students who are failed but their result is incomplete.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|At the prompt type&lt;br /&gt;
&lt;br /&gt;
“'''grep -in “fail” grepdemo.txt'''&amp;lt;nowiki&amp;gt;” [&amp;lt;/nowiki&amp;gt;enter]&lt;br /&gt;
|If we want to see the line number in the file at which the enlisted entries are, we have the '''minus n''' option&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|Let us clear the '''prompt'''. Now type&lt;br /&gt;
&lt;br /&gt;
&amp;quot;'''grep space -in space within double quote &amp;quot;fail&amp;quot; after the double quotes space grepdemo.txt'''&lt;br /&gt;
&lt;br /&gt;
Press '''Enter'''.&lt;br /&gt;
&lt;br /&gt;
The line number is displayed.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|At the prompt type&lt;br /&gt;
&lt;br /&gt;
“'''grep -i “ankit saraf” grepdemo.txt'''&amp;lt;nowiki&amp;gt;” [&amp;lt;/nowiki&amp;gt;enter]&lt;br /&gt;
|The patterns so far have been of single word.&lt;br /&gt;
&lt;br /&gt;
We may have multiword patterns as well. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But the entire pattern must be within '''quotes'''.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|So type:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''grep space minus i space''' within double quotes '''ankit space saraf after the double quotes space grepdemo.txt '''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Press '''Enter'''.&lt;br /&gt;
&lt;br /&gt;
We see that '''Ankit Saraf''''s record is displayed.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|At the prompt type&lt;br /&gt;
&lt;br /&gt;
“'''grep -i “fail” grepdemo.txt notpass.txt'''&amp;lt;nowiki&amp;gt;” [&amp;lt;/nowiki&amp;gt;enter]&lt;br /&gt;
|We can also find patterns in multiple files.For this&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Type:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''grep space minus i space''' within double quotes '''fail after the double quotes space grepdemo.txt space notpass.txt''' &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Press Enter'''.&lt;br /&gt;
&lt;br /&gt;
The output is displayed.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|With multiple files, '''grep''' will write the name of the file in which the entry was found.&lt;br /&gt;
&lt;br /&gt;
'''grepdemo.txt'''  '''notpass.txt'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|Highlight the relevant records.&lt;br /&gt;
|These are the records from '''notpass.txt''' file and these are the records from '''grepdemo.txt''' file.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|At the prompt type&lt;br /&gt;
&lt;br /&gt;
“'''grep -c “Fail” grepdemo.txt”''' &amp;lt;nowiki&amp;gt;[&amp;lt;/nowiki&amp;gt;enter]&lt;br /&gt;
|Suppose we may only want to know the '''number of matches '''or '''count''' .&lt;br /&gt;
&lt;br /&gt;
For that, we have the '''minus c''' option&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 So Type:&lt;br /&gt;
&lt;br /&gt;
'''grep space minus c space''' within double quotes '''Fail'''  with a capital F after the  space grepdemo.txt'''&lt;br /&gt;
&lt;br /&gt;
Press '''Enter'''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This will give us the '''count''' of number of lines matched.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|This brings us to the end of this tutorial.&lt;br /&gt;
&lt;br /&gt;
Let us summarize.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| style=&amp;quot;border:1pt solid #000000;padding:0.176cm;&amp;quot;| Display Slide 6&lt;br /&gt;
&lt;br /&gt;
Summary&lt;br /&gt;
| style=&amp;quot;border:1pt solid #000000;padding:0.176cm;&amp;quot;| In this tutorial we learnt,&lt;br /&gt;
&lt;br /&gt;
*To see the content of a file&lt;br /&gt;
**eg. '''cat filename'''&lt;br /&gt;
*To list the entries of a particular stream&lt;br /&gt;
**eg. '''grep “computers” grepdemo.txt'''&lt;br /&gt;
*To ignore cases&lt;br /&gt;
**eg. '''grep -i “computers” grepdemo.txt'''&lt;br /&gt;
*Lines that do not match the pattern&lt;br /&gt;
**eg. '''grep -iv “pass” grepdemo.txt'''&lt;br /&gt;
*To list the line numbers with the entries&lt;br /&gt;
**eg.''' grep -in “fail” grepdemo.txt'''&lt;br /&gt;
*To store the result in another file&lt;br /&gt;
**eg. '''grep -iv “pass” grepdemo.txt &amp;gt; notpass.txt'''&lt;br /&gt;
*To know the count&lt;br /&gt;
**eg.''' grep -c “Fail” grepdemo.txt'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|Display Slide 7&lt;br /&gt;
|As an assignment,&lt;br /&gt;
&lt;br /&gt;
Explore some other commands like '''-E, + and ?'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|Display Slide 8&lt;br /&gt;
&lt;br /&gt;
Acknowledgement Slide&lt;br /&gt;
|Watch the video available at the link shown below.&lt;br /&gt;
&lt;br /&gt;
It summarises the Spoken Tutorial project.&lt;br /&gt;
&lt;br /&gt;
If you do not have good bandwidth, you can download and watch it.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|Display Slide 9&lt;br /&gt;
&lt;br /&gt;
Spoken Tutorial Workshops&lt;br /&gt;
|The Spoken Tutorial Project Team&lt;br /&gt;
&lt;br /&gt;
*Conducts workshops using spoken tutorials&lt;br /&gt;
*Gives certificates to those who pass an online test&lt;br /&gt;
&lt;br /&gt;
For more details, please write to&lt;br /&gt;
&lt;br /&gt;
'''contact@spoken-tutorial.org'''&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|Display Slide 10&lt;br /&gt;
&lt;br /&gt;
Acknowledgement&lt;br /&gt;
|Spoken Tutorial Project is a part of the Talk to a Teacher project&lt;br /&gt;
&lt;br /&gt;
It is supported by the National Mission on Education through ICT, MHRD, Government of India&lt;br /&gt;
&lt;br /&gt;
More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|&lt;br /&gt;
|This is Ashwini Patil from IIT Bombay signning off. Thank you for joining.&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>PoojaMoolya</name></author>	</entry>

	</feed>