Difference between revisions of "Linux-Ubuntu/C3/Text-Editing-using-sed/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 3: Line 3:
 
'''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.  
  
  
Line 44: Line 44:
 
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.
 
|| The following code files are required to practice this tutorial.
 
|| The following code files are required to practice this tutorial.
 +
 
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 80: Line 81:
 
The letter '''s''' stands for substitution.
 
The letter '''s''' stands for substitution.
  
This pattern matches bothuppercase '''Kumar''' and lowercase '''kumar'''.
+
This pattern matches both uppercase '''Kumar''' and lowercase '''kumar'''.
  
 
The word '''Roy''' is the replacement text.
 
The word '''Roy''' is the replacement text.
Line 90: Line 91:
 
|| Press '''Enter'''.
 
|| Press '''Enter'''.
 
Show the output on the terminal
 
Show the output on the terminal
||  
+
||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.
Line 105: Line 105:
 
The original file '''sed demo dot t x t '''is not modified.
 
The original file '''sed demo dot t x t '''is not modified.
 
|-  
 
|-  
|| '''Slide 6'''
 
  
'''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:'''
 
'''sed 's/pattern/replacement/g' filename'''
 
||
 
* 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.
 
This is the general syntax.
 
|-
 
 
|| Type
 
|| Type
 
'''sed 's/[kK]umar/Roy/g' seddemo.txt'''
 
'''sed 's/[kK]umar/Roy/g' seddemo.txt'''
Line 131: Line 111:
  
 
'''Highlight the output'''
 
'''Highlight the output'''
|| Type this command and press '''Enter.'''
+
|| Type this command.
 +
 
 +
 
 +
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.
 +
 
 +
Now press '''Enter.'''
  
 
Output shows, both occurrences in the fourth line are replaced.
 
Output shows, both occurrences in the fourth line are replaced.
Line 139: Line 134:
 
and press Enter.Case-insensitive matching.
 
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'''.
 +
 
The '''I''' option makes the pattern match '''case insensitive'''.
 
The '''I''' option makes the pattern match '''case insensitive'''.
 +
 
Observe that the output is the same as the previous command.
 
Observe that the output is the same as the previous command.
 +
 
Both methods can be used to produce the same result.
 
Both methods can be used to produce the same result.
 
|-  
 
|-  
|| '''Slide 7'''
+
'''Multiple Substitutions using -e'''
+
* sed allows performing more than one operation in a single command.
+
* The -e option is used to specify multiple sed expressions.
+
* Each -e represents one sed command.
+
* Commands are executed in the order they are written.
+
* This is useful when multiple substitutions are required on the same input.
+
 
+
||
+
* '''sed '''allows performing more than one operation in a single command.
+
* The '''hyphen e '''option is used to specify multiple '''sed''' expressions.
+
* Each '''hyphen e '''represents one '''sed '''command.
+
* Commands are executed in the order they are written.
+
* This is useful when multiple substitutions are required on the same input.
+
|-
+
 
||  
 
||  
 
|| Let us see how to replace '''electronics''' with '''electrical '''and '''civil''' with '''metallurgy.'''
 
|| Let us see how to replace '''electronics''' with '''electrical '''and '''civil''' with '''metallurgy.'''
Line 177: Line 162:
 
Highlight the replaced words in the output
 
Highlight the replaced words in the output
 
|| Now, press '''Enter'''.
 
|| Now, press '''Enter'''.
 +
 
Observe that words are replaced.
 
Observe that words are replaced.
 
|-  
 
|-  
Line 185: Line 171:
 
'''sed '/Anirban/s/computers/mathematics/g' seddemo.txt'''
 
'''sed '/Anirban/s/computers/mathematics/g' seddemo.txt'''
 
|| Type this command and press '''Enter'''.
 
|| Type this command and press '''Enter'''.
 +
 
Observe that computers is changed to mathematics.
 
Observe that computers is changed to mathematics.
 
|-  
 
|-  
 
|| '''Deleting Lines using sed'''
 
|| '''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 203: Line 191:
 
'''>'''
 
'''>'''
 
'''nonElectronics.txt'''
 
'''nonElectronics.txt'''
 +
 
|| Here, '''slash electronics slash''' is a context address that matches lines containing electronics.
 
|| Here, '''slash electronics slash''' is a context address that matches lines containing electronics.
'''d '''means deletes the matched lines
+
 
 +
'''d '''means deletes the matched lines.
 +
 
 
'''seddemo dot t x t '''is the input file.
 
'''seddemo dot t x t '''is the input file.
greater than means redirects output to a file
+
 
 +
greater than means redirects output to a file.
 +
 
 +
 
 
'''non Electronics dot t x t''' is the output file.
 
'''non Electronics dot t x t''' is the output file.
 
Press '''Enter'''.
 
Press '''Enter'''.
Line 214: Line 208:
 
'''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'''.
 +
 
The file shows only those records that are not in the electronics stream.
 
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.
 
You may wonder why substitutions are not reflected in the text file.
 +
 
This is because all substitution results are displayed only on the terminal.
 
This is because all substitution results are displayed only on the terminal.
 +
 
The original text file is not modified unless explicitly instructed.
 
The original text file is not modified unless explicitly instructed.
 
|-  
 
|-  
Line 233: Line 231:
 
* 1 is the line number
 
* 1 is the line number
 
* i is the insert command
 
* i is the insert command
*'''Student Information '''is the text to be inserted
+
* '''Student Information '''is the text to be inserted
 
|-  
 
|-  
 
|| Press Enter.
 
|| Press Enter.
Line 239: Line 237:
 
'''Student Information'''
 
'''Student Information'''
 
|| Press '''Enter.'''
 
|| Press '''Enter.'''
 +
 
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'''
 
|| '''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 248: Line 248:
 
'''sed '1i Student Information\n2026\nRecords' seddemo.txtpress Enter'''
 
'''sed '1i Student Information\n2026\nRecords' seddemo.txtpress Enter'''
 
|| Now let us type this command.
 
|| Now let us type this command.
 +
 
Press '''Enter.'''
 
Press '''Enter.'''
 
|-  
 
|-  
 
|| Point to '''\n'''
 
|| Point to '''\n'''
 
|| Notice '''backslash n '''between the string '''Information''' and '''2026'''.
 
|| 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'''.
 
'''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.
 
Similarly after 2026, new string '''Records''' is printed in the third line.
 
|-  
 
|-  
Line 267: Line 270:
 
* Redirect '''sed '''output to an output file
 
* Redirect '''sed '''output to an output file
 
|| With this we come to the end of this tutorial.
 
|| With this we come to the end of this tutorial.
 +
 
Let us summarise.
 
Let us summarise.
 
|-  
 
|-  
Line 285: Line 289:
  
 
'''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 14:11, 13 March 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.
  • Use the g flag for global substitution.
  • Execute multiple sed commands using the hyphen e option.
  • Apply substitution based on context.
  • Insert and remove lines from a file.
  • Redirect sed output to an output file.
Slide 3

System Requirements

To record this tutorial, I am using
  • Ubuntu OS version 24 point 04
  • Bash version 5 point 1 point 16;
Slide 4 Pre-requisites

https://EduPyramids.org

Learners should have
  • Ubuntu version 24 point 04 and
  • Bash version 5 point 1 point 16
  • For the prerequisite Linux tutorials please visit this website.
Slide 5

Code files The following code files are required to practice this tutorial.

  1. seddemo.txt
  2. sed-commands.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 both uppercase 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.

Type

sed 's/[kK]umar/Roy/g' seddemo.txt Press Enter

Highlight the output

Type this command.


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.

Now 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.

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.
  • hyphen e allows using more than one sed command in a single line.
  • s slash electronics slash electrical slash g replaces all occurrences of electronics with electrical.
  • s slash civil slash metallurgy slash g replaces all occurrences of civilwith metallurgy.
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:
  • 1 is the line number
  • i is the insert command
  • Student Information is the text to be inserted
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:

  • Perform text substitution using sed command
  • Use the g flag for global substitution
  • Execute multiple sed commands using the -e option
  • Apply substitution based on context
  • Insert and remove lines from a file
  • Redirect sed output to an output file
With this we come to the end of this tutorial.

Let us summarise.

Slide 9

'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.
  • Perform both substitutions in a single command using the -e option.
  • Delete all lines that belong to the civil department.
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.

Contributors and Content Editors

Ketkinaina, Madhurig