Linux-Ubuntu/C3/Introduction-to-the-Stream-Editor-sed/English

From Script | Spoken-Tutorial
Revision as of 12:22, 25 January 2026 by Ketkinaina (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Introduction to the Stream Editor sed.

Author: EduPyramids.

Keywords: stream editor, sed, display, substitute, insert, delete, context addressing, Line addressing, edupyramids, video tutorial.

Visual Cue Narration
Slide 1

Title Slide

Welcome to this spoken tutorial on Introduction to the Stream Editor sed.
Slide 2

Learning Objectives

In this tutorial, we will learn to:
  • Use sed as a stream editor to process text.
  • Print specific lines from a file.
  • Apply line-based addressing techniques.
  • Use context-based addressing with pattern matching.
  • Redirect matched output to files.
Slide 3

System Requirements

To record this tutorial, I am using
  • Ubuntu OS version 24 point 04 and
  • Bash version 5 point 1 point 16.
Slide 4

Pre-requisites

https://EduPyramids.org

To follow this tutorial,
  • Learners should have Ubuntu version 24 point 04.
  • Bash version 5 point 1 point 16 and
  • Should know the basics of Linux terminal.
  • 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.
Slide 6

Introduction to sed

  • sed is one of the most versatile tools available in Linux.
  • It is a stream editor used to process text line by line.
  • sed can function both as a display tool and an editing tool.
  • It supports editing operations such as insertion, substitution and deletion.
  • By default, sed sends its output to the standard output.
  • The original file remains unchanged unless sed is explicitly instructed to modify it.
Open the file seddemo.txt

Highlight the lines, marks and student details.

Let us start with a few examples.

We will see how to print lines using the sed command.

I have a file called sed demo dot t x t in the home directory.

This file is provided in the code files folder. Please download it and move it to your Home directory.

Let us view its contents.

This file contains entries such as roll no, name, stream, marks, result and the stipend amount.

Press Ctrl + Alt + T keys. Let us open the terminal.
Type:sed '2p' seddemo.txt Let’s print the second line of the file.

Type,

sed space single quote 2 p single quote space sed demo dot t x t.

Highlight 2

Highlight p

Press Enter

highlight the output.

Hover your mouse on the second line of the output

Here, 2 specifies the location, which is the second line.The letter p specifies the action, which is printing.

Press Enter.

Observe the output.

It shows the entire file but the second line appears twice.

This is the default behavior of sed.

sed prints every line by default

The p option prints the specified line again.

At the prompt type:

sed -n '2p' seddemo.txt Press Enter

To print only the second line, type this command and press Enter.
Highlight -n

Highlight 2

Highlight p

Highlight sed demo.txt

Observe that only the second line is printed.
  • The hyphen n option suppresses the default output of sed.
  • 2 specifies the line number to display or edit.
  • p specifies the action to perform.
  • It tells sed to print the selected line.
  • sed demo dot t x t specifies the input file name.

This is the general syntax of the sed command.

At the prompt type:sed -n '$p' seddemo.txt Press Enter.

Highlight -n

Highlight $ sign

Now let us print the last line.Type this command and press Enter. We see that the last line is displayed.

The hyphen n suppresses automatic printing of all lines.

Dollar refers to the last line of the file.

At the prompt

sed -n '3,6p' seddemo.txt press Enter

Highlight the output

Let us just print the entries from the third line to the sixth line of the file.

Type this command and press Enter.

The output displays the lines from the third line to the sixth line.

Typesed -n '3,6!p' seddemo.txt

Press Enter

Show the file on one side and the output comparison

If we want to print all lines except the third to the sixth lines.

Type this command.Any action can be reversed by placing an exclamation mark before the command.

This allows us to select the complementary set of lines.

Press Enter.

The output displays all lines except lines 3 to 6.

So far, we have specified the lines in a file on which an action applies.

This method is known as line addressing.

Slide 7

Line addressing

Line addressing identifies lines using their position in the file.

It is one way to select specific lines for an action.

Another method is called context addressing.

Slide 8

Context addressing

  • Context addressing selects lines based on their content.
  • Lines are matched using a word, string, or pattern.
  • Only the lines that match the pattern are processed.
  • It is useful when line numbers are unknown or variable.
  • Regular expressions can be used for flexible and powerful matching.
  • Commonly used for searching and filtering text in files.
Let us see an example.
Highlight the lines with word computers in the text editor- show the text file seddemo.txt.

At the promptsed -n '/[cC]omputers/p' seddemo.txt

Highlight -n

highlight /[cC]omputers/

highlight p

Press Enter.

highlight /[cC]omputers/

Suppose we want to print the lines containing the word computers.

Go to the Terminal.

Type the following command.

hyphen n suppresses default output.

This is the Context address.

It matches lines containing the word computers with small c and capital C.

p, prints the matched lines.

Press Enter.

Observe the output.

The lines containing the word computers are displayed.

We enclose parts of the pattern within square brackets.

This allows us to match any one character present inside the brackets.

When using a pattern in sed, it must be written between forward slashes.

Type the command:

sed -n '/[cC]omputers/w computer-student' seddemo.txt

press Enter.

Open file and show the computer-student

Let us now see how to write the matched output to a file using the w option.

Type the following command.

and Press Enter.

Output is redirected to a file instead of getting displayed on the terminal.

This is useful for extracting specific lines into a separate file.

All matching lines are saved in the file computer hyphen student.

Type

cat computer-student andPress Enter

Let us view the content of the computer hyphen student file.

type cat space computer hyphen student and press Enter.

We see that lines that match the given pattern are in the file.

Type the following command.sed -n -e '/electronics/w electro' -e '/civil/w civil' seddemo.txtPress Enter

Highlight -e

Highlight /electronics/w electro

Highlight /civil/w civil

Show the two files created

Let us see how we can use multiple sed expressions in a single command. This allows us to write different matched lines to separate files.

Type this command and press Enter.

The hyphen e option allows combining multiple sed commands in a single line.

slash electronics slash w electro writes lines matching electronics to electro file.

slash civil slash w civil writes lines matching civil to civil file.

After running the command, two text files, electro and civil, are created with the matched lines.

cat electro Press Enter

Highlight the contents

cat civil

Press Enter

Highlight the contents

To see what they contain, type cat space electro and press Enter.

This displays the entries with the word electronics.

Let us see the contents of the file civil

Type cat space civil and press Enter.

This displays the entries having the word civil.

We will see more sed commands in the upcoming tutorial.
Slide 9

Summary

In this tutorial, we have learnt to:

  • Use sed as a stream editor to process text
  • Print specific lines from a file
  • Apply line-based addressing techniques
  • Use context-based addressing with pattern matching
  • Redirect matched output to files
With this we come to the end of this tutorial.

Let us summarise.

Slide 10

Assignment

As an assignment,

Using the text file seddemo.txt,

  1. Print only the first five lines of the file.
  2. Print all lines that do not contain the word "failed".
  3. Write all lines containing the word student into a file named student-list.txt.
As an assignment, please do the following.
Slide 11

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