Linux-Ubuntu/C3/Text-Editing-using-sed/English

From Script | Spoken-Tutorial
Revision as of 17:31, 26 January 2026 by Ketkinaina (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
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.# seddemo.txt

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

  • 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

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

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

Type cat nonElectronics.txt

press Enter 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

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