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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
'''Title of script''': The grep command and Regular Expressions
+
'''Title of script''': More on 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 16: Line 16:
  
  
| style="border:1pt solid #000000;padding:0.106cm;"| Welcome to this spoken tutorial on some more on '''grep''' command
+
| style="border:1pt solid #000000;padding:0.106cm;"| Welcome to the spoken tutorial on more on '''grep''' command
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.106cm;"| Display Slide 2
 
| style="border:1pt solid #000000;padding:0.106cm;"| Display Slide 2
 
| style="border:1pt solid #000000;padding:0.106cm;"| In this tutorial we will learn some more grep commands.
 
| style="border:1pt solid #000000;padding:0.106cm;"| In this tutorial we will learn some more grep commands.
 +
 +
Through some examples
  
 
|-
 
|-
Line 28: Line 30:
 
'''Ubuntu Linux''' 12.04''' Operating System''' and
 
'''Ubuntu Linux''' 12.04''' Operating System''' and
  
'''GNU BASH''' version 4.2.24(1)
+
'''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 34: Line 36:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.106cm;"| Display Slide 4
 
| style="border:1pt solid #000000;padding:0.106cm;"| Display Slide 4
| style="border:1pt solid #000000;padding:0.106cm;"| To practice this tutorial,
+
| style="border:1pt solid #000000;padding:0.106cm;"| As prerequisites
  
 
You should know Basics of linux terminal.
 
You should know Basics of linux terminal.
Line 52: Line 54:
 
Then we have to use the '''-e''' option.
 
Then we have to use the '''-e''' option.
  
 +
I will use the same example grepdemo.txt
  
 
Say we want to find information for those who are either in '''civil''' or '''electronics'''.
 
Say we want to find information for those who are either in '''civil''' or '''electronics'''.
Line 61: Line 64:
  
 
'''Press Enter.'''
 
'''Press Enter.'''
 +
 +
The output is displayed.
  
 
|-
 
|-
Line 84: Line 89:
 
'''Press Enter.'''
 
'''Press Enter.'''
  
 +
The output is displayed.
  
 
But there can be many other ways in which we can write the name.
 
But there can be many other ways in which we can write the name.
Line 123: Line 129:
  
 
'''grepdemo.txt'''<nowiki> [</nowiki>enter]
 
'''grepdemo.txt'''<nowiki> [</nowiki>enter]
| style="border:1pt solid #000000;padding:0.106cm;"| Like to match '''choudhury''' we may use the command
+
| style="border:1pt solid #000000;padding:0.106cm;"| To match '''choudhury''' we may type at the prompt
 
+
type at the prompt
+
  
  
Line 133: Line 137:
 
'''Press Enter.'''
 
'''Press Enter.'''
  
It still does not match '''choudhuree'''
+
The output is displayed.
 +
 
 +
It still does not match '''choudhuree''' with double e.
  
 
|-
 
|-
Line 142: Line 148:
  
  
Like to match any digit we simply write<nowiki> [0-9].</nowiki>
+
Suppose we like to match any digit we simply write<nowiki> [0-9].
 +
 
 +
One out of this group of characters is matched. </nowiki>
  
 
|-
 
|-
Line 176: Line 184:
  
 
'''Press Enter.'''
 
'''Press Enter.'''
 +
 +
The output is displayed.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.106cm;"| Display Slide 9
+
| style="border:1pt solid #000000;padding:0.106cm;"|
 
| style="border:1pt solid #000000;padding:0.106cm;"| The '''dot “.”'''
 
| style="border:1pt solid #000000;padding:0.106cm;"| The '''dot “.”'''
  
Line 199: Line 209:
  
 
'''Press Enter.'''
 
'''Press Enter.'''
 +
 +
The output is displayed.
  
  
Line 205: Line 217:
  
 
As it would match 5 or more letter words.
 
As it would match 5 or more letter words.
 
 
  
  
Line 258: Line 268:
 
'''Press Enter '''
 
'''Press Enter '''
  
 +
The output is displayed.
  
 
This brings me to the end of this tutorial.  
 
This brings me to the end of this tutorial.  
 +
 +
Let us summarize.
  
 
|-
 
|-

Latest revision as of 15:57, 14 December 2013

Title of script: More on 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 more on grep command
Display Slide 2 In this tutorial we will learn some more grep commands.

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

You should be aware of grep.

For relevant tutorials please visit our website:

http://spoken-tutorial.org

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.

I will use the same example grepdemo.txt

Say we want to find information for those who are either in civil or electronics.


We need to type:

grep space hyphen e space in double quotes electronics space hyphen e space in double quotes civil space grepdemo.txt

Press Enter.

The output is displayed.

At the prompt type

grep -ie “choudhury” -ie “chowdhari” grepdemo.txt” [enter]

Say you need to look into people whose title is choudhury.


The problem is different people spell their titles in different way.

So what is the solution?.


In such cases we can use hyphen e option with hyphen i.


Type:


grep space hyphen ie space in double quotes choudhury space hyphen ie space in double quotes chowdhari space grepdemo.txt

Press Enter.

The output is displayed.

But there can be many other ways in which we can write the name.


How many -e options can we give.


Obviously there needs to be a better way and that way is through Regular expressions.

Display Slide 5 A regular expression provides a concise and flexible means for matching strings of text.


Such as particular characters, words or patterns of characters.

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 .


Only one out of this group of characters is matched.

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]

To match choudhury we may type at the prompt


grep space hyphen i space in double quotes ch[ao][uw]dh[ua]r[yi] space grepdemo.txt


Press Enter.

The output is displayed.

It still does not match choudhuree with double e.

Display Slide 7 If we want to specify a large range then write:

first letter dash last letter of the range


Suppose we like to match any digit we simply write [0-9]. One out of this group of characters is matched.

Display Slide 8


The * refers to 0 or more occurrences of the immediately preceding character.


For example ab* can match a,ab,abb,abbb etc.



At the prompt

grep -i “m[ei]*raa*” grepdemo.txt


[enter]


So to match the students name whose name is Mira

We write at the prompt:


grep space hyphen i space in double quotes m[ei] asterisk r a a asterisk space grepdemo.txt


Press Enter.

The output is displayed.

The dot “.”

This regular expression matches any one character.

At the prompt

grep “M...” grepdemo.txt


[enter]

Say we want to search any words that are 4 letters long and start with M.


We would simply write


grep space in double quotes M... space space grepdemo.txt

Press Enter.

The output is displayed.


Here the space within the quotes is important.


As it would match 5 or more letter words.


Display Slide 10 We may be specific about where in the line we want to search for our pattern.


It may be at the beginning of the line.


For that we have the caret sign.

At the prompt


grep “^A” grepdemo.txt


[enter]

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:


grep in double quotes caret A then space grepdemo.txt


Press Enter.

Display Slide 11


At the prompt, type:

grep “[78]...$ ” grepdemo.txt


[enter]

Similarly to match a pattern at the end of the file, we have the dollar sign.


To find stipends between 7000 to 8999 we have to write:

grep space in double quotes [78]...dolar sign space grepdemo.txt

Press Enter

The output is displayed.

This brings me to the end of this tutorial.

Let us summarize.

Display Slide 12

Summary

In this tutorial we learnt,


To match more than one pattern

eg. grep -e “electronics” -e “civil” grepdemo.txt


To check a word that has different spelling

eg. grep -ie “choudhury” -ie “chowdhari” grepdemo.txt


Character class

eg. grep -i “ch[ao][uw]dh[ua]r[yi]”

grepdemo.txt


The use of *

eg. grep -i “m[ei]*raa*” grepdemo.txt


To match any one character using dot

eg. grep “M...” grepdemo.txt


To match a pattern at the beginning of the file

eg. grep “^A” grepdemo.txt


To match a pattern at the end of the file

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.

Contributors and Content Editors

Ashwini