Linux/C3/More-on-sed-command/English
Title of script: sed - The stream editor
Author: Sachin Patil
Keywords: sed, substitute, insert, delete
Visual Cue | Narration |
Display slide 1
|
Welcome to this spoken tutorial on some more on sed. |
Display slide 2
Learning objectives |
In this tutorial we tutorial we will learn some more sed commands. |
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(1) Please note, GNU bash version 4 or above is recommended to practice this tutorial. |
Display slide 4
Prerequisites
|
To practice this tutorial,
You should know Basics of linux terminal. You should be aware of sed tool. For relevant tutorials please visit our website: |
The major use of sed is substitution.
ie replacing some pattern in the input with something else. | |
Type
cat seddemo.txt
|
Lets first look at the original file seddemo.txt
|
Type
|
Notice the word Kumar occurs two times in the fourth and once in the sixth line.
Type sed space 's/[kK]umar/Roy/' space seddemo.txt Press Enter |
Show the output
|
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 |
Type
|
Type
sed space 's/[kK]umar/Roy/g' 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
-e ‘s/civil/metallurgy/g' seddemo.txt
|
Type:
sed space -e ‘s/electronics/electrical/g’ -e ‘s/civil/metallurgy/g’ space seddemo.txt And press Enter. |
Type
|
Now we want to change the stream of Anirban from computers to mathematics.
sed space '/Anirban/s/computers/mathematics/g' space seddemo.txt Press Enter. |
[highlight text inside single quotes]
|
Let us understand what this is.
First we write sed then in single quotes we write the pattern to be matched.
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
|
For that we have the d flag.
sed space (in single quotes) (front slash) /electronics/d space seddemo.txt space greater than sign space nonelectronics.txt Press Enter |
cat nonelectronics.txt
|
To see the contents type:
cat space nonelectronics.txt and press enter. |
Type
|
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. |
Infact 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
|
So in that case we would write
Press Enter Notice \n between the string ‘Information’ and ‘2011’. \n prints 2011 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. |