Linux/C3/The-sed-command/English
Sachin p:
review doneTitle of script: sed - The stream editor
Author: Sachin Patil
Keywords: stream, editor, display, substitute, insert, delete
|
|
Display slide 1 | Welcome to this spoken tutorial on the sed - the stream editor. |
Display slide 2
Learning objectives |
In this tutorial we tutorial we will learn usage of sed command. |
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 For relevant tutorials please visit our website: |
Display slide 5
Introduction |
sed is one of the most versatile tool in Linux.
Its a stream editor. sed finds some pattern of text in a particular location of a file. Then it behave as a display or editing function. It performs editing like insertion, substitution, deletion of the matched text. |
Open the file seddemo.txt
|
Let us first start with some examples.
Let us see how to print using the sed command. We have a file called seddemo.txt in home directory. Let us view its content . |
At the prompt
|
Now suppose we want to print the second line of the file seddemo.txt.
Type: sed space (in single quotes) ‘2p’ space seddemo.txt Press Enter |
Here 2 denotes the location which is the second line.
p denotes the action, which is printing(p). Look at the output. It shows the entire file but see that the second line is printed twice. This is the default behaviour of the action p. | |
At the prompt
|
To only print the second line
type: sed space (minus) -n space (in single quotes) 2p space seddemo.txt Press Enter. |
Highlight
|
-n stands for ‘silent mode’ which will suppress all unnecessary output.
Then we give the location in the stream that we want to edit or display.
|
At the prompt
[enter]
|
To print the last line
sed space (minus) -n space (in single quotes) (dollar) $p space seddemo.txt
|
At the prompt
|
To print from 3rd to 6th line
type: sed space (minus) -n space (in single quotes) ‘3 (comma) ,6p’ space seddemo.txt
|
At the prompt
|
Any of the actions can be reversed by using the exclamation mark before the action.
Say if we had to print all lines except from 3rd to 6th we would type: sed space (minus) -n space (in single quotes) ‘3 (comma) ,6 (exclamation mark) !p space seddemo.txt
|
Display slide 6 | So far, we specified the lines in the file on which the action needs to be taken.
This is one way of addressing called Line Addressing. But if we want to take actions on lines that contain a particular word. For such issues the other way of addressing is Context Addressing. We can use regular expressions. Let us see an example. |
At the prompt
|
Say we want to print those lines which have the word computers.
sed space (minus) -n space (in single quotes) (front slash)(opening angle bracket) (opening angle bracket) (front slash) ‘/[cC]omputers/p space seddemo.txt Press Enter. The lines where the word computers are displayed. When we need to match patterns the pattern needs to be typed between slashes. |
At the prompt
|
We can print it in file as well using the w option.
Type at the prompt:
Press Enter. Now all the matching lines would be transferred to the file computer_student. |
At the prompt
cat computer_student
|
To view the content of computer_student
type: cat space computer_student Press Enter. |
At the prompt
‘/civil/w civil' seddemo.txt”
|
We can also have patterns that we can write to different files.
Type: sed space (minus) -n space (minus) -e space (in single quotes) (front slash) ‘/electronics/w space electro’ (minus) -e space (in single quotes) (front slash) ‘/civil/w space civil’ space seddemo.txt
Press Enter This would create two files electro and civil. |
cat electro
[enter]
[enter] |
To see what they contain type:
cat space electro Press Enter Then type: cat space civil And press Enter |
Now we will see some more set of commands in another tutorial.
I will use the same program file. | |
Display slide 7
Summary |
Let us summarize,
In this tutorial we learnt, sed. To print using sed. Line Addressing. Context Addressing. |
Assignment | As an assignment,
Use the same text file seddemo.txt. Try to print records from 6th to 12th line. |
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. |