BOSS-Linux/C3/The-sed-command/English
Sachin p:
review doneTitle of script: sed - The stream editor
Author: Sachin Patil and Anirban
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. We will do this through some examples. |
Display slide 3
System Requirement |
To record this tutorial, I am using
Linux 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 For relevant tutorials please visit our website which is as shown. |
Display slide 5
Introduction Let us start with an introduction to sed. |
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. It perform some display or editing function. Editing like insertion, substitution, deletion in matched text. |
Open the file seddemo.txt
|
Let us start with some examples.
We will see how to print using the sed command. I have a file called seddemo.txt in home directory. Let us view its content. In this file we have some enteries like roll no, name, stream, marks, pass or fail and the stipend amount.
|
At the prompt
|
Now suppose we want to print the second line of the file.
For this we need to oepn the terminal by pressing Now 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 (within single quotes) 2p space seddemo.txt Press Enter. |
Highlight
We see only the second line is printed. |
-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]
|
Now let us print the last line of the file.
Let me clear the prompt
Now come back to the text editor. |
At the prompt
|
Suppose we want to print the enteries from 3rd to 6th
For this we need to type on the terminal: sed space (minus) -n space (in single quotes) ‘3 (comma) ,6p’ space seddemo.txt
The output is displayed from the third line to the sixth line. |
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
The output is displayed. Let us switch back to the slides. Line addressing and context addressing. |
Display slide 6 | So far, we specified the lines in the file on which the action needs to be taken.
This is known as line addressing. Address specified by the line numbers. This is one way of addressing. Another way of addressing is Context addressing Lines that contain particular context say a particular word. If we want to take actions on lines that contain a particular word we use context addressing. Regular expressions can be used. Let us see an example. Come back to our editor. |
At the prompt
|
Say we want to print those lines which have the word computers.
Come back to our temrinal. Type: sed space (minus) -n space (in single quotes) (front slash)(opening square bracket small c) (opening square bracket) (front slash) ‘/[cC]omputers/p space seddemo.txt Press Enter. We see the lines with the word computers is displayed. We write pattern within square brackets. This is to match any one or both of the characters within square brackets. When we need to match patterns the pattern needs to be typed between front slashes. |
At the prompt
|
We can print it in file as well using the w option.
For this type:
Press Enter. Now all the matching lines would be transferred to the file computer_student.txt.
|
At the prompt
cat computer_student
|
Let us 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.txt’ (minus) -e space (in single quotes) (front slash) ‘/civil/w space civil.txt’ space seddemo.txt Press Enter -e is used to combine multiple methods Press Enter This would create two files electro.txt and civil.txt. |
cat electro
[enter]
[enter] |
To see what they contain type:
cat space electro.txt This will display the enteries with the word electronics. Let us see the content of civil file. Then type: cat space civil.txt press Enter This will display the enteries having the word civil. |
We will see some more set of commands in another tutorial.
I will use the same program. This bring us to the end of the tutorial. Let us switch back to our slides | |
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. |