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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 16: Line 16:
  
  
| style="border:1pt solid #000000;padding:0.106cm;"| Welcome to the spoken tutorial on more on sed.
+
| style="border:1pt solid #000000;padding:0.106cm;"| Welcome to the spoken tutorial on '''More on sed.'''
  
 
|-
 
|-
Line 22: Line 22:
  
 
Learning objectives
 
Learning objectives
| style="border:1pt solid #000000;padding:0.106cm;"| In this tutorial we tutorial we will learn some more on '''sed '''commands.
+
| style="border:1pt solid #000000;padding:0.106cm;"| In this tutorial, we will learn some more '''sed '''commands, through some examples.
 
+
Through some examples.
+
  
 
|-
 
|-
Line 35: Line 33:
 
| style="border:1pt solid #000000;padding:0.106cm;"| To record this tutorial, I am using
 
| style="border:1pt solid #000000;padding:0.106cm;"| 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.
  
 
|-
 
|-
Line 48: Line 45:
  
  
| style="border:1pt solid #000000;padding:0.106cm;"| As prerequisites
+
| style="border:1pt solid #000000;padding:0.106cm;"| As prerequisites,
  
You should know Basics of linux terminal.
+
You should know basics of '''Linux terminal.'''
  
 
You should be aware of sed tool.
 
You should be aware of sed tool.
Line 60: Line 57:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.106cm;"|  
 
| style="border:1pt solid #000000;padding:0.106cm;"|  
| style="border:1pt solid #000000;padding:0.106cm;"| The major use of sed is substitution.
+
| style="border:1pt solid #000000;padding:0.106cm;"| The major use of '''sed''' is substitution.
  
 
Replacing some pattern in the input with something else.
 
Replacing some pattern in the input with something else.
Line 75: Line 72:
 
<nowiki>[ </nowiki>Point to the fourth and sixth line ]
 
<nowiki>[ </nowiki>Point to the fourth and sixth line ]
 
| style="border:1pt solid #000000;padding:0.106cm;"| Let's first look at the original file '''seddemo.txt'''
 
| style="border:1pt solid #000000;padding:0.106cm;"| Let's first look at the original file '''seddemo.txt'''
 
 
  
  
Line 87: Line 82:
  
 
<nowiki>[</nowiki>enter]
 
<nowiki>[</nowiki>enter]
| style="border:1pt solid #000000;padding:0.106cm;"| Notice the word '''Kumar''' occurs two times in the fourth and once in the sixth line.
+
| style="border:1pt solid #000000;padding:0.106cm;"| Notice the word '''Kumar''' occurs two times in the fourth line and once in the sixth line.
  
  
If you want to replace all occurrences of the '''‘kumar’''' with '''‘Roy’'''
+
If you want to replace all occurrences of '''‘Kumar’''' with '''‘Roy’''', type on the '''terminal'''
  
Type on the terminal
+
'''sed space''' within single quote ''''s front slash / opening square bracket [k capital K closing square bracket ]umar front slash/ Roy front slash/'''' after the quotes '''space seddemo.txt'''
  
'''sed''' space within single quote ''''s<nowiki>front slash / opening square bracket [</nowiki>k capital K closing square bracket ]umar slash/ Roy slash/'''' after the quotes space '''seddemo.txt'''
+
Press '''Enter'''.
 
+
'''Press Enter'''
+
  
 
|-
 
|-
Line 102: Line 95:
  
  
<nowiki>[</nowiki>point to 4th line of both output and file]
+
[point to 4th line of both output and file]
 
| style="border:1pt solid #000000;padding:0.106cm;"| Notice the fourth line.
 
| style="border:1pt solid #000000;padding:0.106cm;"| Notice the fourth line.
  
Only the first occurrence of '''kumar''' is changed to '''Roy''', not the second one.
+
Only the first occurrence of '''Kumar''' is changed to '''Roy''', not the second one.
  
Sixth line had one occurrence of the word '''kumar''' and this is replaced now.
+
Sixth line had one occurrence of the word '''Kumar''' and this is replaced now.
  
So, we see only the first entry of the lines has been changed.
+
|-
 +
| style="border:1pt solid #000000;padding:0.106cm;"|
 +
| style="border:1pt solid #000000;padding:0.106cm;"| So, we see only the first entry of the lines has been changed.
  
 
This is because, by default, the first matched entry of any line is substituted.
 
This is because, by default, the first matched entry of any line is substituted.
  
To substitute all the matched entries we need to use the flag ''' g''' option.
+
|-
 +
| style="border:1pt solid #000000;padding:0.106cm;"|
 +
| style="border:1pt solid #000000;padding:0.106cm;"| To substitute all the matched entries we need to use the flag ''' g''' option.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.106cm;"| Type
+
| style="border:1pt solid #000000;padding:0.106cm;"|Type
  
  
'''sed 's<nowiki>/[</nowiki>kK]umar/Roy/g' seddemo.txt'''
+
'''sed 's /[ kK]umar/Roy/g' seddemo.txt'''
  
  
 
<nowiki>[</nowiki>enter]
 
<nowiki>[</nowiki>enter]
| style="border:1pt solid #000000;padding:0.106cm;"| Type
+
| style="border:1pt solid #000000;padding:0.106cm;"| Let me clear the '''prompt. ''' Type
  
'''sed''' space within single quotes ''''s<nowiki>/[</nowiki>kK]umar/Roy/g'''' after the single quote space '''seddemo.txt'''
+
'''sed space within single quotes 's /[ kK]umar/Roy/g'''' after the single quote '''space seddemo.txt'''
  
And '''press Enter'''
+
And press '''Enter'''
  
 
Now, both entries of the fourth line are replaced.
 
Now, both entries of the fourth line are replaced.
 
We can make multiple substitutions in one go as well.
 
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.106cm;"|  
 
| style="border:1pt solid #000000;padding:0.106cm;"|  
| style="border:1pt solid #000000;padding:0.106cm;"| Say we want to replace the words '''electronics''' with the word '''electrical'''
+
| style="border:1pt solid #000000;padding:0.106cm;"|We can make multiple substitutions in one go as well.
 +
 
 +
Say we want to replace the words '''electronics''' with the word '''electrical'''
  
 
And '''civil''' with '''metallurgy''' in the file '''seddemo.txt'''
 
And '''civil''' with '''metallurgy''' in the file '''seddemo.txt'''
Line 154: Line 151:
 
| style="border:1pt solid #000000;padding:0.106cm;"| Type:
 
| style="border:1pt solid #000000;padding:0.106cm;"| Type:
  
'''sed''' space '''-e space within single quotes ‘s/electronics/electrical/g’ -e''' '''‘s/civil/metallurgy/g’''' after the single quote space '''seddemo.txt'''
+
'''sed space -e space within single quotes ‘s/electronics/electrical/g’ -e ‘s/civil/metallurgy/g’''' after the single quote ''' space seddemo.txt'''
  
And '''press Enter.'''
+
And press '''Enter.'''
  
 
You can see that the words are replaced.
 
You can see that the words are replaced.
Line 168: Line 165:
  
 
<nowiki>[</nowiki>enter]
 
<nowiki>[</nowiki>enter]
| style="border:1pt solid #000000;padding:0.106cm;"| Now we want to change the stream of '''Anirban '''from '''computers''' to '''mathematics.'''
+
| style="border:1pt solid #000000;padding:0.106cm;"| Now we want to change the '''stream''' of '''Anirban '''from '''computers''' to '''mathematics.'''
  
  
 
In such case we would need to type:
 
In such case we would need to type:
  
'''sed''' space within single quotes ''''/Anirban/s/computers/mathematics/g' ''' after the single quotes space '''seddemo.txt'''
+
'''sed space''' within single quotes ''''/Anirban/s/computers/mathematics/g' ''' after the single quotes '''space seddemo.txt'''
  
 
Press '''Enter.'''
 
Press '''Enter.'''
  
We see that the stream is changed
+
We see that the '''stream''' is changed.
  
  
Line 196: Line 193:
 
| style="border:1pt solid #000000;padding:0.106cm;"| Let us understand what this is.
 
| style="border:1pt solid #000000;padding:0.106cm;"| Let us understand what this is.
  
First we write sed then in single quotes we write the pattern to be matched.
+
First we write '''sed''' then in single quotes we write the pattern to be matched.
  
  
This is Anirban, now after the slash comes the operation.
+
This is '''Anirban'''.
  
This is s that stands for substitution as we have already seen.
+
Now after the slash comes the operation.
  
Then we mention the pattern to be replaced which is computers
+
This is '''s''' that stands for substitution, as we have already seen.
  
Then the new word that would be substituted which is mathematics.
+
Then we mention the pattern to be replaced which is '''computers'''.
 +
 
 +
Then the new word that would be substituted, which is '''mathematics'''.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.106cm;"|  
 
| style="border:1pt solid #000000;padding:0.106cm;"|  
| style="border:1pt solid #000000;padding:0.106cm;"| We can also use sed to add  or delete lines in the file.
+
| style="border:1pt solid #000000;padding:0.106cm;"| We can also use '''sed''' to add  or delete lines in the file.
  
Say we want to select those lines which do not have stream as '''electronics.'''
+
Say we want to select those lines which do not have '''stream''' as '''electronics.'''
  
 
|-
 
|-
Line 224: Line 223:
  
  
| style="border:1pt solid #000000;padding:0.106cm;"| For that we have the '''d '''flag.
+
| style="border:1pt solid #000000;padding:0.106cm;"| For that, we have the '''d '''flag.
  
  
 
type:
 
type:
  
'''sed''' space (within single quotes) (front slash) /electronics/d after the single quotes space '''seddemo.txt''' space greater than sign space '''nonelectronics.txt'''
+
'''sed space''' (within single quotes) '''(front slash) /electronics/d''' after the single quotes '''space seddemo.txt space greater than sign space nonelectronics.txt'''
 
+
'''Press Enter'''
+
  
 +
Press '''Enter'''.
  
  
Line 240: Line 238:
  
 
<nowiki>[</nowiki>enter]
 
<nowiki>[</nowiki>enter]
| style="border:1pt solid #000000;padding:0.106cm;"| To see the contents type:
+
| style="border:1pt solid #000000;padding:0.106cm;"| To see the contents, type:
  
cat space '''nonelectronics.txt'''
+
'''cat space nonelectronics.txt'''
  
and press enter.
+
and press '''Enter.
  
 
|-
 
|-
Line 254: Line 252:
  
 
<nowiki>[</nowiki>enter]
 
<nowiki>[</nowiki>enter]
| style="border:1pt solid #000000;padding:0.106cm;"| Say at the start of the file we want to add a line Student Information.
+
| style="border:1pt solid #000000;padding:0.106cm;"| Say, at the start of the file we want to add a line '''Student Information'''.
  
 
For that we have the '''i''' action.
 
For that we have the '''i''' action.
  
We need to type:
+
|-
 +
| style="border:1pt solid #000000;padding:0.106cm;"|
 +
| style="border:1pt solid #000000;padding:0.106cm;"| We need to type:
  
sed space in single quotes 1i space''' '''Student Information' '''seddemo.txt '''
+
'''sed space''' in single quotes ''''1i space Student Information' seddemo.txt '''
  
And '''press''' '''Enter.'''
+
And press '''Enter.'''
  
 
You can see the output.
 
You can see the output.
Line 268: Line 268:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.106cm;"|  
 
| style="border:1pt solid #000000;padding:0.106cm;"|  
| style="border:1pt solid #000000;padding:0.106cm;"| Infact we can enter multiple lines like this.
+
| style="border:1pt solid #000000;padding:0.106cm;"| In fact, we can enter multiple lines like this.
  
Say we want to add the two lines we would do it in a similar way.
+
Say, we want to add the two lines we would do it in a similar way.
  
Along with student information we also want to add academics in the next year.
+
Along with '''Student Information''' we also want to add '''academics''' in the next year.
  
  
Line 286: Line 286:
  
  
sed space within single quotes 1i space Student Information'''\n'''2013' after the quotes space seddemo.txt  
+
'''sed space''' within single quotes '''i space Student Information \n 2013'''' after the quotes '''space seddemo.txt'''
  
'''Press Enter'''
+
Press '''Enter'''
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.106cm;"|
 +
| style="border:1pt solid #000000;padding:0.106cm;"|
 
Notice '''\n '''between the string ‘Information’ and ‘2013’.
 
Notice '''\n '''between the string ‘Information’ and ‘2013’.
  

Latest revision as of 15:41, 16 December 2013

Title of script: More on sed

Author: Sachin Patil and Anirban

Keywords: sed, substitute, insert, delete


Visual Cue Narration
Display slide 1


Welcome to the spoken tutorial on More on sed.
Display slide 2

Learning objectives

In this tutorial, we will learn some more sed commands, through some examples.
Display slide 3

System Requirement


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

Prerequisites


As prerequisites,

You should know basics of Linux terminal.

You should be aware of sed tool.

For relevant tutorials please visit our website:

http://spoken-tutorial.org

The major use of sed is substitution.

Replacing some pattern in the input with something else.

Type

cat seddemo.txt


[enter]


[ Point to the fourth and sixth line ]

Let's first look at the original file seddemo.txt


Type


sed 's/[kK]umar/Roy/' seddemo.txt


[enter]

Notice the word Kumar occurs two times in the fourth line and once in the sixth line.


If you want to replace all occurrences of ‘Kumar’ with ‘Roy’, type on the terminal

sed space within single quote 's front slash / opening square bracket [k capital K closing square bracket ]umar front slash/ Roy front slash/' after the quotes space seddemo.txt

Press Enter.

Show the output


[point to 4th line of both output and file]

Notice the fourth line.

Only the first occurrence of Kumar is changed to Roy, not the second one.

Sixth line had one occurrence of the word Kumar and this is replaced now.

So, we see only the first entry of the lines has been changed.

This is because, by default, the first matched entry of any line is substituted.

To substitute all the matched entries we need to use the flag g option.
Type


sed 's /[ kK]umar/Roy/g' seddemo.txt


[enter]

Let me clear the prompt. Type

sed space within single quotes 's /[ kK]umar/Roy/g' after the single quote space seddemo.txt

And press Enter

Now, both entries of the fourth line are replaced.

We can make multiple substitutions in one go as well.

Say we want to replace the words electronics with the word electrical

And civil with metallurgy in the file seddemo.txt

Type


sed -e 's/electronics/electrical/g’

-e ‘s/civil/metallurgy/g' after the single quote space seddemo.txt


[enter]


Type:

sed space -e space within single quotes ‘s/electronics/electrical/g’ -e ‘s/civil/metallurgy/g’ after the single quote space seddemo.txt

And press Enter.

You can see that the words are replaced.

Type


sed '/Anirban/s/computers/mathematics/' seddemo.txt


[enter]

Now we want to change the stream of Anirban from computers to mathematics.


In such case we would need to type:

sed space within single quotes '/Anirban/s/computers/mathematics/g' after the single quotes space seddemo.txt

Press Enter.

We see that the stream is changed.


[highlight text inside single quotes]


[highlight ‘Anirban’]


[highlight ‘s’]


[highlight ‘computers’]


[highlight ‘mathematics’]

Let us understand what this is.

First we write sed then in single quotes we write the pattern to be matched.


This is Anirban.

Now after the slash comes the operation.

This is s that stands for substitution, as we have already seen.

Then we mention the pattern to be replaced which is computers.

Then the new word that would be substituted, which is mathematics.

We can also use sed to add or delete lines in the file.

Say we want to select those lines which do not have stream as electronics.

Type


sed '/electronics/d' seddemo.txt > nonelectronics.txt


[enter]


For that, we have the d flag.


type:

sed space (within single quotes) (front slash) /electronics/d after the single quotes space seddemo.txt space greater than sign space nonelectronics.txt

Press Enter.


cat nonelectronics.txt


[enter]

To see the contents, type:

cat space nonelectronics.txt

and press Enter.

Type


sed '1i Student Information' seddemo.txt


[enter]

Say, at the start of the file we want to add a line Student Information.

For that we have the i action.

We need to type:

sed space in single quotes '1i space Student Information' seddemo.txt

And press Enter.

You can see the output.

In fact, we can enter multiple lines like this.

Say, we want to add the two lines we would do it in a similar way.

Along with Student Information we also want to add academics in the next year.


Type


sed '1i Student Information\n2011' seddemo.txt


[enter]

So in that case we would write


sed space within single quotes i space Student Information \n 2013' after the quotes space seddemo.txt

Press Enter

Notice \n between the string ‘Information’ and ‘2013’.

\n prints 2013 in the next line after ‘Student Information’

This brings me to the end of this tutorial.
Summary Let us summarize,

In this tutorial we learnt,

Substitution

Replacement

Insertion

Assignment As an assignment,

Take the same text file seddemo.txt

And try to replace or substitute name Ankit with Ashish

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 sighning off.

Thank you for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey