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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 16: Line 16:
  
 
|-
 
|-
|00:11
+
|00:12
 
|To record this tutorial, I am using
 
|To record this tutorial, I am using
  
 
|-
 
|-
 
|00:15
 
|00:15
|*'''Ubuntu Linux''' 12.04''' Operating System'''  
+
|*''' Linux''' ''' Operating System'''  
  
 
|-
 
|-
|00:20
+
|00:18
 
| and *'''GNU BASH''' version 4.2.24
 
| and *'''GNU BASH''' version 4.2.24
  
 
|-
 
|-
|00:24
+
|00:22
|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 practise this tutorial.
  
 
|-
 
|-
|00:32
+
|00:30
|As prerequisitesYou should know basics of '''Linux terminal'''
+
|As prerequisites You should know basics of '''Linux terminal'''
  
 
|-
 
|-
|00:36
+
|00:34
 
|For relevant tutorials, please visit our website which is as shown.
 
|For relevant tutorials, please visit our website which is as shown.
  
 
|-
 
|-
|00:41
+
|00:39
 
|First let us know about regular expressions.
 
|First let us know about regular expressions.
  
 
|-
 
|-
|00:45
+
|00:43
 
|*Regular expressions are pattern matching techniques.
 
|*Regular expressions are pattern matching techniques.
  
 
|-
 
|-
|00:50
+
|00:47
 
|*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.
  
 
|-
 
|-
|00:56
+
|00:54
 
|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  
 
   
 
   
 
|-
 
|-
| 01:02
+
| 01:01
 
|  Or  To find a keyword in a paragraph or a line, we use '''grep command'''
 
|  Or  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'''.
  
 
|-
 
|-
|01:11
+
|01:08
 
|'''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.
  
 
|-
 
|-
|01:17
+
|01:15
 
|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.
  
 
|-
 
|-
|01:23
+
|01:22
 
|If filename is missing, '''grep''' searches for the patterns in the standard input.
 
|If filename is missing, '''grep''' searches for the patterns in the standard input.
  
 
|-
 
|-
|01:30
+
|01:28
 
|I will demonstrate usage of '''grep''' using a demo file called '''grepdemo.txt'''
 
|I will demonstrate usage of '''grep''' using a demo file called '''grepdemo.txt'''
  
 
|-
 
|-
|01:37
+
|01:35
 
|Let us see the content of the file.
 
|Let us see the content of the file.
  
 
|-
 
|-
|01:40
+
|01:37
 
|This is a file that has 13 entries.  
 
|This is a file that has 13 entries.  
  
 
|-
 
|-
|01:44
+
|01:41
 
|Each entry has 6 fields roll number, name, stream, marks, and stipend amount.  
 
|Each entry has 6 fields roll number, name, stream, marks, and stipend amount.  
  
 
|-
 
|-
|01:52
+
|01:50
 
|The fields are separated by a bar, which is called a '''delimiter'''.
 
|The fields are separated by a bar, which is called a '''delimiter'''.
  
 
|-
 
|-
|01:56
+
|01:55
 
|Let us see how '''grep''' works.
 
|Let us see how '''grep''' works.
  
 
|-
 
|-
|02:00
+
|01:58
 
|Say we want to use '''grep command''', 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.
  
 
|-
 
|-
|02:07
+
|02:04
 
|For this we have to open the '''terminal'''.
 
|For this we have to open the '''terminal'''.
  
 
|-
 
|-
|02:10
+
|02:07
|So press '''CTRL + ALT and T''' keys simultaneously on your keyboard.
+
 
+
|-
+
|02:16
+
 
|Now type on the '''terminal''':
 
|Now type on the '''terminal''':
  
 
|-
 
|-
|02:18
+
|02:09
 
|'''grep space (within double quotes) computers after the double quotes space  grepdemo .txt'''
 
|'''grep space (within double quotes) computers after the double quotes space  grepdemo .txt'''
  
 
|-
 
|-
|02:27
+
|02:18
 
|Press Enter
 
|Press Enter
  
 
|-
 
|-
|02:28
+
|02:20
 
|This would enlist those entries where stream is '''computers'''.
 
|This would enlist those entries where stream is '''computers'''.
  
 
|-
 
|-
|02:33
+
|02:25
 
|Now compare the result with the original file.  
 
|Now compare the result with the original file.  
  
 
|-
 
|-
|02:37
+
|02:29
 
|Come back to our text editor.
 
|Come back to our text editor.
  
 
|-
 
|-
|02:40
+
|02:31
 
|We see that the entry for ''' Zubin''' is not enlisted.
 
|We see that the entry for ''' Zubin''' is not enlisted.
  
 
|-
 
|-
|02:45
+
|02:37
 
|Why this is so?
 
|Why this is so?
  
 
|-
 
|-
|02:46
+
|02:38
 
|This is because '''grep''' searched for the pattern '''“computers”''' with small '''c'''  
 
|This is because '''grep''' searched for the pattern '''“computers”''' with small '''c'''  
  
 
|-
 
|-
|02:52
+
|02:43
 
|While for '''Zubin''', the stream is “Computers” with a capital '''C'''.
 
|While for '''Zubin''', the stream is “Computers” with a capital '''C'''.
  
 
|-
 
|-
|02:57
+
|02:48
 
|The pattern matching is '''case sensitive'''.
 
|The pattern matching is '''case sensitive'''.
  
 
|-
 
|-
|03:00
+
|02:51
 
|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'''.
  
 
|-
 
|-
|03:06
+
|02:58
|From back to our terminal Now Type:
+
|Come back to our terminal Now 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'''
  
 
|-
 
|-
|03:20
+
|03:12
 
|Press ''' Enter.'''
 
|Press ''' Enter.'''
  
 
|-
 
|-
|03:21
+
|03:13
 
|This will now enlist all the four entries.
 
|This will now enlist all the four entries.
  
 
|-
 
|-
|03:25
+
|03:17
 
|So we saw, '''grep''' enlists only those lines of files that match a given pattern.
 
|So we saw, '''grep''' enlists only those lines of files that match a given pattern.
  
 
|-
 
|-
|03:32
+
|03:23
 
|We may do the reverse.
 
|We may do the reverse.
  
 
|-
 
|-
|03:34
+
|03:26
 
|It is possible to make '''grep''' enlist only those lines that '''do not''' match the pattern.
 
|It is possible to make '''grep''' enlist only those lines that '''do not''' match the pattern.
  
 
|-
 
|-
|03:40
+
|03:32
 
|For that we have the '''minus v''' option.
 
|For that we have the '''minus v''' option.
  
 
|-
 
|-
|03:43
+
|03:35
 
|Say, we want to enlist those entries of students who have not passed.
 
|Say, we want to enlist those entries of students who have not passed.
  
 
|-
 
|-
|03:48
+
|03:40
 
|We can also store this result in another file
 
|We can also store this result in another file
  
 
|-
 
|-
|03:52
+
|03:43
 
|For this type:
 
|For this type:
 
'''grep space minus iv space''' Within double quotes '''pass after the double quotes space grepdemo.txt space greater than sign space notpass.txt '''
 
'''grep space minus iv space''' Within double quotes '''pass after the double quotes space grepdemo.txt space greater than sign space notpass.txt '''
  
 
|-
 
|-
|04:11
+
|04:02
 
|Press '''Enter.'''
 
|Press '''Enter.'''
  
 
|-
 
|-
|04:12
+
|04:04
 
|To see the content of file, type: '''cat space notpass.txt '''
 
|To see the content of file, type: '''cat space notpass.txt '''
  
 
|-
 
|-
|04:20
+
|04:11
 
|Press '''Enter'''.
 
|Press '''Enter'''.
  
 
|-
 
|-
|04:21
+
|04:12
 
|The output is displayed.
 
|The output is displayed.
  
 
|-
 
|-
|04:24
+
|04:15
 
|Now at the '''prompt''' type:
 
|Now at the '''prompt''' type:
  
 
|-
 
|-
|04:26
+
|04:17
 
|''grep space minus i space''' within double quotes '''fail''' after the double quotes space '''grepdemo.txt'''
 
|''grep space minus i space''' within double quotes '''fail''' after the double quotes space '''grepdemo.txt'''
  
 
|-
 
|-
|04:37
+
|04:28
 
|And Press '''Enter.'''
 
|And Press '''Enter.'''
  
 
|-
 
|-
|04:38
+
|04:30
 
|This is different.
 
|This is different.
 
    
 
    
 
|-
 
|-
|04:41
+
|04:32
 
|This will include those students who are failed but their result is incomplete.
 
|This will include those students who are failed but their result is incomplete.
  
 
|-
 
|-
|04:46
+
|04:37
 
|If we want to see the line number in the file at which the enlisted entries are, we have the '''minus n''' option
 
|If we want to see the line number in the file at which the enlisted entries are, we have the '''minus n''' option
  
 
|-
 
|-
|04:54
+
|04:46
 
|Let us clear the '''prompt'''.  
 
|Let us clear the '''prompt'''.  
  
 
|-
 
|-
|04:58
+
|04:49
 
|  Now type "'''grep space -in space within double quote "fail" after the double quotes space grepdemo.txt'''
 
|  Now type "'''grep space -in space within double quote "fail" after the double quotes space grepdemo.txt'''
  
 
|-
 
|-
|05:09
+
|05:01
 
|Press '''Enter'''.
 
|Press '''Enter'''.
  
 
|-
 
|-
|05:11
+
|05:03
 
|The line number is displayed.
 
|The line number is displayed.
  
 
|-
 
|-
|05:15
+
|05:06
 
|The patterns so far have been of single word.
 
|The patterns so far have been of single word.
  
 
|-
 
|-
|05:18
+
|05:10
 
|We may have multiword patterns as well.  
 
|We may have multiword patterns as well.  
  
 
|-
 
|-
|05:21
+
|05:13
 
|But the entire pattern must be within '''quotes'''.
 
|But the entire pattern must be within '''quotes'''.
  
 
|-
 
|-
|05:24
+
|05:17
 
|So type: '''grep space minus i space'''within double quotes '''ankit space saraf after the double quotes space grepdemo.txt '''
 
|So type: '''grep space minus i space'''within double quotes '''ankit space saraf after the double quotes space grepdemo.txt '''
  
 
|-
 
|-
|05:38
+
|05:29
 
|Press '''Enter'''.
 
|Press '''Enter'''.
  
 
|-
 
|-
|05:40
+
|05:31
 
|We see that '''Ankit Saraf''''s record is displayed.
 
|We see that '''Ankit Saraf''''s record is displayed.
  
 
|-
 
|-
|05:44
+
|05:35
 
|We can also find patterns in multiple files.
 
|We can also find patterns in multiple files.
  
 
|-
 
|-
|05:48
+
|05:39
 
|For this Type:
 
|For this Type:
 
'''grep space minus i space''' within double quotes '''fail after double quotes space grepdemo.txt space notpass.txt'''  
 
'''grep space minus i space''' within double quotes '''fail after double quotes space grepdemo.txt space notpass.txt'''  
  
 
|-
 
|-
|06:03
+
|05:55
 
|'''Press Enter'''.
 
|'''Press Enter'''.
  
 
|-
 
|-
|06:04
+
|05:57
 
|The output is displayed.
 
|The output is displayed.
  
 
|-
 
|-
|06:07
+
|05:59
 
|With multiple files, '''grep''' will write the name of the file in which the entry was found.'''grepdemo.txt''' and '''notpass.txt'''
 
|With multiple files, '''grep''' will write the name of the file in which the entry was found.'''grepdemo.txt''' and '''notpass.txt'''
  
 
|-
 
|-
|06:18
+
|06:10
 
|These are the records from '''notpass.txt''' file and these are the records from '''grepdemo.txt''' file.
 
|These are the records from '''notpass.txt''' file and these are the records from '''grepdemo.txt''' file.
  
 
|-
 
|-
|06:26
+
|06:18
 
|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''' .
  
 
|-
 
|-
|06:31
+
|06:23
 
|For that, we have the '''minus c''' option
 
|For that, we have the '''minus c''' option
  
 
|-
 
|-
|06:35
+
|06:27
 
| So Type: '''grep space minus c space'''within double quotes '''Fail  with a capital F after the quotes space grepdemo.txt'''
 
| So Type: '''grep space minus c space'''within double quotes '''Fail  with a capital F after the quotes space grepdemo.txt'''
  
 
|-
 
|-
|06:48
+
|06:40
 
|Press '''Enter'''.
 
|Press '''Enter'''.
  
 
|-
 
|-
|06:50
+
|06:42
 
|This will give us the '''count''' of number of lines matched.
 
|This will give us the '''count''' of number of lines matched.
  
 
|-
 
|-
|06:55
+
|06:46
 
|This brings us to the end of this tutorial.
 
|This brings us to the end of this tutorial.
  
 
|-
 
|-
|06:59
+
|06:51
 
|Let us summarize.
 
|Let us summarize.
  
 
|-
 
|-
| 07:01
+
| 06:53
 
| In this tutorial we learnt,
 
| In this tutorial we learnt,
  
 
|-
 
|-
|07:03
+
|06:55
 
|*To see the content of a file
 
|*To see the content of a file
 
**eg. '''cat filename'''
 
**eg. '''cat filename'''
 
|-
 
|-
|07:07
+
|06:59
 
|*To list the entries of a particular stream
 
|*To list the entries of a particular stream
 
**eg. '''grep “computers” grepdemo.txt'''
 
**eg. '''grep “computers” grepdemo.txt'''
  
 
|-
 
|-
|07:14
+
|07:06
 
|*To ignore cases
 
|*To ignore cases
 
**eg. '''grep -i “computers” grepdemo.txt'''
 
**eg. '''grep -i “computers” grepdemo.txt'''
  
 
|-
 
|-
|07:21
+
|07:14
 
|*Lines that do not match the pattern
 
|*Lines that do not match the pattern
 
**eg. '''grep -iv “pass” grepdemo.txt'''
 
**eg. '''grep -iv “pass” grepdemo.txt'''
  
 
|-
 
|-
|07:30
+
|07:22
 
|*To list the line numbers with the entries
 
|*To list the line numbers with the entries
 
**eg.''' grep -in “fail” grepdemo.txt'''
 
**eg.''' grep -in “fail” grepdemo.txt'''
  
 
|-
 
|-
|07:38
+
|07:31
 
|*To store the result in another file
 
|*To store the result in another file
 
**eg. '''grep -iv “pass” grepdemo.txt > notpass.txt'''
 
**eg. '''grep -iv “pass” grepdemo.txt > notpass.txt'''
  
 
|-
 
|-
|07:50
+
|07:41
 
|And*To know the count
 
|And*To know the count
 
**eg.''' grep -c “Fail” grepdemo.txt'''
 
**eg.''' grep -c “Fail” grepdemo.txt'''
  
 
|-
 
|-
|07:57
+
|07:48
 
|As an assignment,
 
|As an assignment,
  
 
|-
 
|-
|07:58
+
|07:50
 
|Explore some other commands like '''-E, + and ?'''
 
|Explore some other commands like '''-E, + and ?'''
  
 
|-
 
|-
|08:04
+
|07:56
 
|Watch the video available at the link shown below.
 
|Watch the video available at the link shown below.
  
 
|-
 
|-
|08:06
+
|07:58
 
|It summarises the Spoken Tutorial project.
 
|It summarises the Spoken Tutorial project.
  
 
|-
 
|-
|08:10
+
|08:01
 
|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.
  
 
|-
 
|-
|08:14
+
|08:05
 
|The Spoken Tutorial Project Team
 
|The Spoken Tutorial Project Team
  
 
|-
 
|-
|08:16
+
|08:07
 
|*Conducts workshops using spoken tutorials
 
|*Conducts workshops using spoken tutorials
  
 
|-
 
|-
|08:19
+
|08:11
 
|*Gives certificates to those who pass an online test
 
|*Gives certificates to those who pass an online test
  
 
|-
 
|-
|08:23
+
|08:14
 
|For more details, please write to
 
|For more details, please write to
 
'''contact@spoken-tutorial.org'''
 
'''contact@spoken-tutorial.org'''
  
 
|-
 
|-
|08:30
+
|08:21
 
|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
  
 
|-
 
|-
|08:33
+
|08:25
 
|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
  
 
|-
 
|-
|08:40
+
|08:32
 
|More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro
 
|More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro
  
 
|-
 
|-
|08:45
+
|08:36
 
|This is Ashwini Patil from IIT Bombay signning off. Thank you for joining.
 
|This is Ashwini Patil from IIT Bombay signning off. Thank you for joining.
  
 
|}
 
|}

Revision as of 13:00, 12 February 2015

Time Narration
00:01 Welcome to the spoken tutorial on grep command
00:05 In this tutorial we will learn grep command.
00:09 We will do this with the help of some examples.
00:12 To record this tutorial, I am using
00:15 * Linux Operating System
00:18 and *GNU BASH version 4.2.24
00:22 Please note, GNU bash version 4 or above is recommended to practise this tutorial.
00:30 As prerequisites You should know basics of Linux terminal
00:34 For relevant tutorials, please visit our website which is as shown.
00:39 First let us know about regular expressions.
00:43 *Regular expressions are pattern matching techniques.
00:47 *When we have to find out whether a pattern exist in a line, paragraph or a file.
00:54 For ex. If you want to search a phone number in the telephone directory
01:01 Or To find a keyword in a paragraph or a line, we use grep command

Let us move on to grep.

01:08 grep searches for one or more patterns in one or more line, paragraph or a file.
01:15 If filename is not mentioned, grep search for the patterns in the standard input.
01:22 If filename is missing, grep searches for the patterns in the standard input.
01:28 I will demonstrate usage of grep using a demo file called grepdemo.txt
01:35 Let us see the content of the file.
01:37 This is a file that has 13 entries.
01:41 Each entry has 6 fields roll number, name, stream, marks, and stipend amount.
01:50 The fields are separated by a bar, which is called a delimiter.
01:55 Let us see how grep works.
01:58 Say we want to use grep command, to see who are the students in the computers stream.
02:04 For this we have to open the terminal.
02:07 Now type on the terminal:
02:09 grep space (within double quotes) computers after the double quotes space grepdemo .txt
02:18 Press Enter
02:20 This would enlist those entries where stream is computers.
02:25 Now compare the result with the original file.
02:29 Come back to our text editor.
02:31 We see that the entry for Zubin is not enlisted.
02:37 Why this is so?
02:38 This is because grep searched for the pattern “computers” with small c
02:43 While for Zubin, the stream is “Computers” with a capital C.
02:48 The pattern matching is case sensitive.
02:51 To make it case insensitive, we need to use the minus i option with grep.
02:58 Come back to our terminal Now Type:

grep space (minus) i space (within double quotes) “computers” after the double quotes space grepdemo.txt

03:12 Press Enter.
03:13 This will now enlist all the four entries.
03:17 So we saw, grep enlists only those lines of files that match a given pattern.
03:23 We may do the reverse.
03:26 It is possible to make grep enlist only those lines that do not match the pattern.
03:32 For that we have the minus v option.
03:35 Say, we want to enlist those entries of students who have not passed.
03:40 We can also store this result in another file
03:43 For this type:

grep space minus iv space Within double quotes pass after the double quotes space grepdemo.txt space greater than sign space notpass.txt

04:02 Press Enter.
04:04 To see the content of file, type: cat space notpass.txt
04:11 Press Enter.
04:12 The output is displayed.
04:15 Now at the prompt type:
04:17 grep space minus i space' within double quotes fail after the double quotes space grepdemo.txt
04:28 And Press Enter.
04:30 This is different.
04:32 This will include those students who are failed but their result is incomplete.
04:37 If we want to see the line number in the file at which the enlisted entries are, we have the minus n option
04:46 Let us clear the prompt.
04:49 Now type "grep space -in space within double quote "fail" after the double quotes space grepdemo.txt
05:01 Press Enter.
05:03 The line number is displayed.
05:06 The patterns so far have been of single word.
05:10 We may have multiword patterns as well.
05:13 But the entire pattern must be within quotes.
05:17 So type: grep space minus i spacewithin double quotes ankit space saraf after the double quotes space grepdemo.txt
05:29 Press Enter.
05:31 We see that Ankit Saraf's record is displayed.
05:35 We can also find patterns in multiple files.
05:39 For this Type:

grep space minus i space within double quotes fail after double quotes space grepdemo.txt space notpass.txt

05:55 Press Enter.
05:57 The output is displayed.
05:59 With multiple files, grep will write the name of the file in which the entry was found.grepdemo.txt and notpass.txt
06:10 These are the records from notpass.txt file and these are the records from grepdemo.txt file.
06:18 Suppose we may only want to know the number of matches or count .
06:23 For that, we have the minus c option
06:27 So Type: grep space minus c spacewithin double quotes Fail with a capital F after the quotes space grepdemo.txt
06:40 Press Enter.
06:42 This will give us the count of number of lines matched.
06:46 This brings us to the end of this tutorial.
06:51 Let us summarize.
06:53 In this tutorial we learnt,
06:55 *To see the content of a file
    • eg. cat filename
06:59 *To list the entries of a particular stream
    • eg. grep “computers” grepdemo.txt
07:06 *To ignore cases
    • eg. grep -i “computers” grepdemo.txt
07:14 *Lines that do not match the pattern
    • eg. grep -iv “pass” grepdemo.txt
07:22 *To list the line numbers with the entries
    • eg. grep -in “fail” grepdemo.txt
07:31 *To store the result in another file
    • eg. grep -iv “pass” grepdemo.txt > notpass.txt
07:41 And*To know the count
    • eg. grep -c “Fail” grepdemo.txt
07:48 As an assignment,
07:50 Explore some other commands like -E, + and ?
07:56 Watch the video available at the link shown below.
07:58 It summarises the Spoken Tutorial project.
08:01 If you do not have good bandwidth, you can download and watch it.
08:05 The Spoken Tutorial Project Team
08:07 *Conducts workshops using spoken tutorials
08:11 *Gives certificates to those who pass an online test
08:14 For more details, please write to

contact@spoken-tutorial.org

08:21 Spoken Tutorial Project is a part of the Talk to a Teacher project
08:25 It is supported by the National Mission on Education through ICT, MHRD, Government of India
08:32 More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro
08:36 This is Ashwini Patil from IIT Bombay signning off. Thank you for joining.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Sandhya.np14