Linux-Ubuntu/C3/Text-Editing-using-sed/English
Title of script: Text Editing using sed
Author: EduPyramids
Keywords: sed, substitute, insert, delete, global substitution, g flag, case-insensitive, context addressing, Video Tutorial.
| Visual Cue | Narration |
| Slide 1
Title Slide |
Welcome to this spoken tutorial on Text Editing using sed. |
| Slide 2 Learning Objectives | In this tutorial, we will learn to:* Perform text substitution using sed command.
|
| Slide 3
System Requirements |
To record this tutorial, I am using* Ubuntu OS version 24 point 04
|
| Slide 4 Pre-requisites | Learners should have* Ubuntu version 24 point 04 and
|
| Slide 5
Code files The following code files are required to practice this tutorial.# seddemo.txt
These files are provided in the Code Files link of this tutorial page. |
The following code files are required to practice this tutorial.
These files are provided in the Code Files link of this tutorial page. |
| Let us run some examples of sed commands using the terminal. | |
| Type
cat seddemo.txtpress Enter Highlight the word kumar in the lines.Point to the fourth and sixth lines |
Let us first view the contents of the file sed demo dot t x t.
Notice the word Kumar. It appears twice in the fourth line and once in the sixth line. |
| Type
sed 's/[kK]umar/Roy/' seddemo.txt |
Let’s suppose we want to substitute Kumar with Roy.
Type the following command and press Enter. |
| Highlight one by one
sed s [kK]umar Roy forward slashes seddemo.txt |
Let us understand this command.
sed is the stream editor. The letter s stands for substitution. This pattern matches bothuppercase Kumar and lowercase kumar. The word Roy is the replacement text. The forward slashes separate the pattern and the replacement. The file sed demo dot txt is the input file. |
| Press Enter.
Show the output on the terminal |
In the output, the word Kumar is replaced with Roy. |
|
Show the output on the terminal and the file seddemo.txt opened next to it Highlight 4th line of both output and in the file |
But only the first occurrence of the word Kumar is replaced in each line.
This happens because, by default, sed substitutes only the first match in a line. Note the substitution happens only on the terminal output. The original file sed demo dot t x t is not modified. |
| Slide 6
Global Substitution
General syntax: sed 's/pattern/replacement/g' filename |
* By default, sed replaces only the first match in each line.
This is the general syntax. |
| Type
sed 's/[kK]umar/Roy/g' seddemo.txt Press Enter Highlight the output |
Type this command and press Enter.
Output shows, both occurrences in the fourth line are replaced. |
| Type
sed 's/kumar/Roy/Ig' seddemo.txt and press Enter.Case-insensitive matching. |
Type the following command and press Enter.
Instead of specifying lowercase and uppercase letters using brackets, we can use the I option. The I option makes the pattern match case insensitive. Observe that the output is the same as the previous command. Both methods can be used to produce the same result. |
| Slide 7
Multiple Substitutions using -e
|
|
| Let us see how to replace electronics with electrical and civil with metallurgy. | |
| Type
sed -e 's/electronics/electrical/g' -e 's/civil/metallurgy/g' seddemo.txt |
Type this command. |
Let us understand this code.
| |
| press Enter
Highlight the replaced words in the output |
Now, press Enter.
Observe that words are replaced. |
| Context-Based Substitution | Let’s change the stream of Anirban from computers to mathematics. |
| Type
sed '/Anirban/s/computers/mathematics/g' seddemo.txt |
Type this command and press Enter.
Observe that computers is changed to mathematics. |
| Deleting Lines using sed | Let us see how to remove all lines containing the word electronics.
In other words, select those lines which do not have an electronics stream. |
| Type
sed '/electronics/d' seddemo.txt > nonElectronics.txt |
Type this command. |
|
Highlight /electronics/ d seddemo.txt > nonElectronics.txt |
Here, slash electronics slash is a context address that matches lines containing electronics.
d means deletes the matched lines seddemo dot t x t is the input file. greater than means redirects output to a file non Electronics dot t x t is the output file. Press Enter. |
| Open the text editor and show the file.
Show the output |
Let us view the contents of the new file nonElectronics.
The file shows only those records that are not in the electronics stream. You may wonder why substitutions are not reflected in the text file. This is because all substitution results are displayed only on the terminal. The original text file is not modified unless explicitly instructed. |
| Inserting Lines Using sed | Suppose we want to insert a line at the beginning of the file. |
| Type
sed '1i Student Information' seddemo.txt |
Type this command. |
| Highlight one by one
1 i Student Information |
Here:
|
| Press Enter.
Highlight the line: Student Information |
Press Enter.
Output shows the line Student Information inserted at the beginning of the file. |
| Inserting Multiple Lines | Let us see how to insert more than one line in the file seddemo dot t x t.
Student Information in the first line and 2026 in the next line. |
| Type
sed '1i Student Information\n2026\nRecords' seddemo.txtpress Enter |
Now let us type this command.
Press Enter. |
| Point to \n | Notice backslash n between the string Information and 2026.
backslash n moves to a new line and prints 2026 in the next line after Student Information. Similarly after 2026, new string Records is printed in the third line. |
| Slide 8
Summary In this tutorial, we have learnt to:
|
With this we come to the end of this tutorial.
Let us summarise. |
| Slide 9
As an assignment please do the following:
|
As an assignment, please do the following. |
| Slide 10
Thank you |
This Spoken Tutorial is brought to you by EduPyramids Educational Services Private Limited SINE IIT Bombay.Thank you. |