Difference between revisions of "Linux-Ubuntu/C3/Text-Editing-using-sed/English"
Ketkinaina (Talk | contribs) (Created page with " '''Title of script''': ''' Text Editing using sed''' '''Author: EduPyramids''' '''Keywords: sed, substitute, insert, delete, global substitution, g flag, case-insensitive, co...") |
|||
| Line 1: | Line 1: | ||
| − | + | '''Title of script''': ''' Text Editing using sed''' | |
| + | |||
'''Author: EduPyramids''' | '''Author: EduPyramids''' | ||
| − | |||
| + | '''Keywords: sed, substitute, insert, delete, global substitution, g flag, case-insensitive, context addressing, Video Tutorial.''' | ||
| Line 43: | Line 44: | ||
|- | |- | ||
|| | || | ||
| − | | | Let us run some examples of sed commands using the terminal. | + | || Let us run some examples of sed commands using the terminal. |
|- | |- | ||
|| Type | || Type | ||
| Line 115: | Line 116: | ||
|| Type | || Type | ||
'''sed 's/kumar/Roy/Ig' seddemo.txt''' | '''sed 's/kumar/Roy/Ig' seddemo.txt''' | ||
| − | + | and press Enter.Case-insensitive matching. | |
|| Type the following command and press '''Enter.''' | || Type the following command and press '''Enter.''' | ||
Instead of specifying lowercase and uppercase letters using brackets, we can use the '''I option'''. | Instead of specifying lowercase and uppercase letters using brackets, we can use the '''I option'''. | ||
| Line 130: | Line 131: | ||
* This is useful when multiple substitutions are required on the same input. | * This is useful when multiple substitutions are required on the same input. | ||
| − | + | || | |
| − | + | * '''sed '''allows performing more than one operation in a single command. | |
| − | || * '''sed '''allows performing more than one operation in a single command. | + | |
* The '''hyphen e '''option is used to specify multiple '''sed''' expressions. | * The '''hyphen e '''option is used to specify multiple '''sed''' expressions. | ||
* Each '''hyphen e '''represents one '''sed '''command. | * Each '''hyphen e '''represents one '''sed '''command. | ||
| Line 143: | Line 143: | ||
|| Type | || Type | ||
'''sed -e 's/electronics/electrical/g' -e 's/civil/metallurgy/g' seddemo.txt''' | '''sed -e 's/electronics/electrical/g' -e 's/civil/metallurgy/g' seddemo.txt''' | ||
| − | | | + | || Type this command. |
|- | |- | ||
|| | || | ||
| Line 158: | Line 158: | ||
Observe that words are replaced. | Observe that words are replaced. | ||
|- | |- | ||
| − | | | + | || '''Context-Based Substitution''' |
|| Let’s change the '''stream''' of '''Anirban '''from '''computers''' to '''mathematics'''. | || Let’s change the '''stream''' of '''Anirban '''from '''computers''' to '''mathematics'''. | ||
|- | |- | ||
| Line 166: | Line 166: | ||
Observe that computers is changed to mathematics. | Observe that computers is changed to mathematics. | ||
|- | |- | ||
| − | | | + | || '''Deleting Lines using sed''' |
|| Let us see how to remove all lines containing the word '''electronics'''. | || 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. | In other words, select those lines which do not have an electronics stream. | ||
| Line 173: | Line 173: | ||
'''sed '/electronics/d' seddemo.txt > nonElectronics.txt''' | '''sed '/electronics/d' seddemo.txt > nonElectronics.txt''' | ||
| − | | | + | || Type this command. |
|- | |- | ||
|| | || | ||
| Line 189: | Line 189: | ||
Press '''Enter'''. | Press '''Enter'''. | ||
|- | |- | ||
| − | || | + | || Open the text editor and show the file. |
| − | + | ||
'''Show the output''' | '''Show the output''' | ||
|| Let us view the contents of the new file '''nonElectronics'''. | || Let us view the contents of the new file '''nonElectronics'''. | ||
| Line 198: | Line 198: | ||
The original text file is not modified unless explicitly instructed. | 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 | || Type | ||
'''sed '1i Student Information' seddemo.txt''' | '''sed '1i Student Information' seddemo.txt''' | ||
| − | | | + | || Type this command. |
|- | |- | ||
|| Highlight one by one | || Highlight one by one | ||
| Line 210: | Line 210: | ||
'''Student Information''' | '''Student Information''' | ||
|| Here: | || Here: | ||
| − | + | * 1 is the line number | |
| − | + | * i is the insert command | |
| + | *'''Student Information '''is the text to be inserted | ||
|- | |- | ||
|| Press Enter. | || Press Enter. | ||
| Line 219: | Line 220: | ||
Output shows the line '''Student Information''' inserted at the beginning of the file. | 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'''. | || 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. | '''Student Information''' in the first line and '''2026''' in the next line. | ||
| Line 234: | Line 235: | ||
|- | |- | ||
|| '''Slide 8''' | || '''Slide 8''' | ||
| + | |||
'''Summary''' | '''Summary''' | ||
| + | |||
In this tutorial, we have learnt to: | In this tutorial, we have learnt to: | ||
* Perform text substitution using '''sed '''command | * Perform text substitution using '''sed '''command | ||
| Line 240: | Line 243: | ||
* Execute multiple '''sed''' commands using the '''-e''' option | * Execute multiple '''sed''' commands using the '''-e''' option | ||
* Apply substitution based on context | * Apply substitution based on context | ||
| − | |||
* Insert and remove lines from a file | * Insert and remove lines from a file | ||
* Redirect '''sed '''output to an output file | * Redirect '''sed '''output to an output file | ||
| Line 247: | Line 249: | ||
|- | |- | ||
|| '''Slide 9''' | || '''Slide 9''' | ||
| − | As an assignment please do the following:* Using '''sed '''and the file '''seddemo.txt''', replace the word '''computers''' with '''computer-science'''. | + | |
| + | As an assignment please do the following: | ||
| + | |||
| + | * Using '''sed '''and the file '''seddemo.txt''', replace the word '''computers''' with '''computer-science'''. | ||
* Replace '''electronics''' with '''ece''' in all occurrences. | * Replace '''electronics''' with '''ece''' in all occurrences. | ||
* Perform both substitutions in a single command using the '''-e option'''. | * Perform both substitutions in a single command using the '''-e option'''. | ||
* Delete all lines that belong to the '''civil''' department. | * Delete all lines that belong to the '''civil''' department. | ||
| − | + | ||
| − | | | + | || As an assignment, please do the following. |
|- | |- | ||
| − | || '''Slide 10 | + | || '''Slide 10 ''' |
| − | Thank you''' | + | |
| + | '''Thank you''' | ||
|| This Spoken Tutorial is brought to you by EduPyramids Educational Services Private Limited SINE IIT Bombay.Thank you. | || This Spoken Tutorial is brought to you by EduPyramids Educational Services Private Limited SINE IIT Bombay.Thank you. | ||
|- | |- | ||
|} | |} | ||
Revision as of 11:06, 1 February 2026
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. |