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...") |
|||
| (One intermediate revision by the same user not shown) | |||
| 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.''' | '''Keywords: sed, substitute, insert, delete, global substitution, g flag, case-insensitive, context addressing, Video Tutorial.''' | ||
| − | |||
{| border=1 | {| border=1 | ||
| − | | | '''Visual Cue''' | + | || '''Visual Cue''' |
| − | | | '''Narration''' | + | || '''Narration''' |
|- | |- | ||
|| '''Slide 1''' | || '''Slide 1''' | ||
| Line 14: | Line 15: | ||
|- | |- | ||
|| '''Slide 2 Learning Objectives''' | || '''Slide 2 Learning Objectives''' | ||
| − | || In this tutorial, we will learn to:* Perform text substitution using '''sed '''command. | + | || In this tutorial, we will learn to: |
| + | * Perform text substitution using '''sed '''command. | ||
* Use the '''g''' flag for global substitution. | * Use the '''g''' flag for global substitution. | ||
* Execute multiple '''sed''' commands using the '''hyphen e''' option. | * Execute multiple '''sed''' commands using the '''hyphen e''' option. | ||
| Line 20: | Line 22: | ||
* 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. | ||
| − | |||
|- | |- | ||
|| '''Slide 3''' | || '''Slide 3''' | ||
'''System Requirements''' | '''System Requirements''' | ||
| − | || To record this tutorial, I am using* '''Ubuntu OS '''version 24 point 04 | + | || To record this tutorial, I am using |
| + | * '''Ubuntu OS '''version 24 point 04 | ||
* '''Bash''' version 5 point 1 point 16; | * '''Bash''' version 5 point 1 point 16; | ||
|- | |- | ||
|| '''Slide 4 Pre-requisites''' | || '''Slide 4 Pre-requisites''' | ||
https://EduPyramids.org | https://EduPyramids.org | ||
| − | || Learners should have* '''Ubuntu version 24 point 04 '''and | + | || Learners should have |
| + | * '''Ubuntu version 24 point 04 '''and | ||
* '''Bash version 5 '''point '''1 '''point '''16''' | * '''Bash version 5 '''point '''1 '''point '''16''' | ||
* For the prerequisite Linux tutorials please visit this website. | * For the prerequisite Linux tutorials please visit this website. | ||
| Line 36: | Line 39: | ||
|| '''Slide 5''' | || '''Slide 5''' | ||
'''Code files''' | '''Code files''' | ||
| − | The following code files are required to practice this tutorial.# '''seddemo.txt''' | + | The following code files are required to practice this tutorial. |
| + | # '''seddemo.txt''' | ||
# '''sed-commands.txt''' | # '''sed-commands.txt''' | ||
These files are provided in the Code Files link of this tutorial page. | These files are provided in the Code Files link of this tutorial page. | ||
| Line 43: | Line 47: | ||
|- | |- | ||
|| | || | ||
| − | | | Let us run some examples of sed commands using the terminal. | + | || Let us run some examples of sed commands using the terminal. |
|- | |- | ||
| − | || Type | + | || Type '''cat seddemo.txtpress Enter'''. |
| − | '''cat seddemo.txtpress Enter''' | + | |
| − | Highlight the word kumar in the lines.Point to the fourth and sixth lines | + | 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.''' | || Let us first view the contents of the file '''sed demo dot t x t.''' | ||
| + | |||
Notice the word '''Kumar'''. | Notice the word '''Kumar'''. | ||
| + | |||
It appears twice in the fourth line and once in the sixth line. | It appears twice in the fourth line and once in the sixth line. | ||
|- | |- | ||
|| Type | || Type | ||
'''sed 's/[kK]umar/Roy/' seddemo.txt''' | '''sed 's/[kK]umar/Roy/' seddemo.txt''' | ||
| + | |||
|| Let’s suppose we want to substitute '''Kumar''' with '''Roy.''' | || Let’s suppose we want to substitute '''Kumar''' with '''Roy.''' | ||
| + | |||
Type the following command and press '''Enter'''. | Type the following command and press '''Enter'''. | ||
|- | |- | ||
| Line 67: | Line 77: | ||
'''sed''' is the stream editor. | '''sed''' is the stream editor. | ||
| + | |||
The letter '''s''' stands for substitution. | The letter '''s''' stands for substitution. | ||
| + | |||
This pattern matches bothuppercase '''Kumar''' and lowercase '''kumar'''. | This pattern matches bothuppercase '''Kumar''' and lowercase '''kumar'''. | ||
| + | |||
The word '''Roy''' is the replacement text. | The word '''Roy''' is the replacement text. | ||
| + | |||
The forward slashes separate the pattern and the replacement. | The forward slashes separate the pattern and the replacement. | ||
| + | |||
The file '''sed demo dot txt''' is the input file. | The file '''sed demo dot txt''' is the input file. | ||
|- | |- | ||
| Line 78: | Line 93: | ||
In the output, the word '''Kumar''' is replaced with '''Roy'''. | 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. |
| − | 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 | + | Highlight 4th line of both output and in the file. |
| + | |||
|| But only the first occurrence of the word '''Kumar''' is replaced in each line. | || 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. | This happens because, by default, '''sed''' substitutes only the first match in a line. | ||
| + | |||
Note the substitution happens only on the terminal output. | Note the substitution happens only on the terminal output. | ||
| + | |||
The original file '''sed demo dot t x t '''is not modified. | The original file '''sed demo dot t x t '''is not modified. | ||
|- | |- | ||
| Line 89: | Line 108: | ||
'''Global Substitution''' | '''Global Substitution''' | ||
| − | * | + | * By default, sed replaces only the first match in each line. |
| − | * | + | |
| − | * | + | * To replace all occurrences in a line, we use the g flag. |
| − | * | + | * The letter g stands for global substitution. |
| − | * | + | * When the g flag is used, sed searches the entire line for matches. |
| − | * | + | * Every matching pattern in that line is replaced. |
| + | * The g flag is written after the substitution command. | ||
'''General syntax:''' | '''General syntax:''' | ||
'''sed 's/pattern/replacement/g' filename''' | '''sed 's/pattern/replacement/g' filename''' | ||
| − | || * By default, '''sed''' replaces only the first match in each line. | + | || |
| + | * By default, '''sed''' replaces only the first match in each line. | ||
* To replace all occurrences in a line, we use the '''g''' flag. | * To replace all occurrences in a line, we use the '''g''' flag. | ||
* The letter '''g''' stands for global substitution. | * The letter '''g''' stands for global substitution. | ||
| Line 111: | Line 132: | ||
'''Highlight the output''' | '''Highlight the output''' | ||
|| Type this command and press '''Enter.''' | || Type this command and press '''Enter.''' | ||
| + | |||
Output shows, both occurrences in the fourth line are replaced. | Output shows, both occurrences in the fourth line are replaced. | ||
|- | |- | ||
|| 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 152: | ||
* 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 164: | ||
|| 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 179: | ||
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 187: | ||
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 194: | ||
'''sed '/electronics/d' seddemo.txt > nonElectronics.txt''' | '''sed '/electronics/d' seddemo.txt > nonElectronics.txt''' | ||
| − | | | + | || Type this command. |
|- | |- | ||
|| | || | ||
| Line 189: | Line 210: | ||
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 219: | ||
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 231: | ||
'''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 241: | ||
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 256: | ||
|- | |- | ||
|| '''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 264: | ||
* 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 270: | ||
|- | |- | ||
|| '''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'''. | + | |
| + | ''''Assignment''' | ||
| + | |||
| + | 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. | ||
|- | |- | ||
|} | |} | ||
Latest revision as of 11:53, 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:
|
| Slide 3
System Requirements |
To record this tutorial, I am using
|
| Slide 4 Pre-requisites | Learners should have
|
| Slide 5
Code files The following code files are required to practice this tutorial.
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 |
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
'Assignment 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. |