Difference between pages "Linux-New/C3/Introduction-to-grep-command/English" and "Linux-New/C3/Introduction-to-the-Stream-Editor-sed/English"

From Script | Spoken-Tutorial
(Difference between pages)
Jump to: navigation, search
(Created page with "'''Title of the Script: Introduction to grep command. '''Author:''' EduPyramids.''' '''Keywords:''' '''grep, pattern matching, case-insensitive, inverse match, multiword sea...")
 
 
Line 1: Line 1:
'''Title of the Script: Introduction to grep command.
+
'''Title of script''': '''Introduction to the Stream Editor sed'''
  
'''Author:''' EduPyramids.'''
+
'''Author: EduPyramids '''
  
'''Keywords:''' '''grep, pattern matching, case-insensitive, inverse match, multiword search, line numbers, count matches, edupyramids, video tutorial.'''
+
'''Keywords: stream editor, sed, display, substitute, insert, delete, context addressing, Line addressing, edupyramids, video tutorial'''.
  
  
Line 14: Line 14:
  
 
'''Title Slide'''
 
'''Title Slide'''
|| Welcome to this spoken tutorial on '''Introduction to grep command'''.
+
|| Welcome to this spoken tutorial on '''Introduction to the Stream Editor sed'''.
 
|-  
 
|-  
|| '''Slide 2 Learning Objectives  '''
+
|| '''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'''
  
|| In this tutorial, we will learn to:* View the content of a file
+
'''Pre-requisites'''
* List entries of a particular stream
+
* Ignore cases while searching
+
* Handle lines that do not match a pattern
+
* Display line numbers with the entries
+
* Store the result in another file
+
* Count the number of matches
+
  
 +
[https://EduPyramids.org/ 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 of '''Linux''' tutorials please visit this website.
 +
|-
 +
|| '''Slide 5'''
  
 +
'''Code Files'''
  
 +
The following code files are required to practice this tutorial.
 +
# '''seddemo.txt.'''
 +
# '''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 3'''
+
|| '''Slide 6'''
  
'''System  Requirements'''
+
'''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.
  
|| To record this tutorial, I am using 
+
I have a file called '''sed demo dot t x t''' in the home directory.
  
'''Ubuntu OS '''version 24 point zero 4
+
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.
 
|-  
 
|-  
|| '''Slide 4Pre-requisites'''
+
|| Type:'''sed '2p' seddemo.txt'''
 +
|| Let’s print the second line of the file.
  
'''https://EduPyramids.org'''
+
Type,
  
 +
'''sed space single quote 2 p single quote space sed demo dot t x t'''.
 +
|-
 +
|| '''Highlight 2'''
  
 +
'''Highlight p'''
  
|| To follow this tutorial,
+
Press '''Enter'''
  
Learners should have '''Ubuntu version 24 point zero 4'''.
+
highlight the output.
  
And should be familiar with basic Linux terminal commands.
+
Hover your mouse on the second line of the output
 +
|| Here, '''2''' specifies the location, which is the second line.
  
For the prerequisite of Linux tutorials please visit this website.
+
The letter '''p''' specifies the action, which is printing.
|-
+
|| '''Slide 5'''
+
  
'''Code Files'''
+
Press '''Enter'''.
  
The following code files are required to practice this tutorial.# '''grepdemo.txt'''
+
Observe the output.
# '''igc-commands.txt'''
+
  
 +
It shows the entire file but the second line appears twice.
  
 +
This is the default behavior of '''sed'''.
  
These files are provided in the Code Files link of this tutorial page.
+
'''sed''' prints every line by default
|| The following code files are required to practice this tutorial.
+
  
 +
The '''p''' option prints the specified line again.
 +
|-
 +
|| At the prompt type:
  
These files are provided in the Code Files link of this tutorial page.
+
'''sed -n '2p' seddemo.txt '''Press''' Enter'''
 +
|| To print only the second line, type this command and press''' Enter'''.
 
|-  
 
|-  
| |
+
|| '''Highlight -n'''
|| First let us learn about regular expressions.
+
|-
+
|| '''Slide 6'''
+
  
'''Regular Expressions'''
+
'''Highlight 2'''
|| * Regular expressions are patterns used to match text.
+
* They help to search and filter information in files.
+
* They can match words, numbers, or character sequences.
+
* In Linux, commands like '''grep''' use regular expressions.
+
  
 +
'''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.
 
|-  
 
|-  
|| '''Slide 7'''
+
|| At the prompt type:'''sed -n '$p' seddemo.txt''' Press''' Enter'''.
  
'''grep command '''
+
'''Highlight -n'''
|| * '''grep''' is a '''Linux''' command used to search text.
+
* It finds lines that match a given pattern.
+
* It works on files or standard input.
+
* It supports regular expressions for searching.
+
  
 +
'''Highlight $ sign'''
 +
|| Now let us print the last line.
  
|-
+
Type this command and''' '''press '''Enter'''.  
|| Download code file.Cut and paste to Home directory.open '''grepdemo.txt '''code file.
+
|| We will now demonstrate the '''grep''' command using the '''grepdemo.txt file'''.
+
  
Download this file and move it to your home directory.
+
We see that the last line is displayed.
|-
+
|| show the entries and point to the separation bar (|)
+
|| This file contains 13 entries.Each entry has a roll number, name, stream, marks, result, and stipend.  
+
  
The fields are separated by a vertical bar, which acts as a '''delimiter'''.
+
The '''hyphen n''' suppresses automatic printing of all lines.
 +
 
 +
Dollar refers to the last line of the file.
 
|-  
 
|-  
|| '''Note: Please type the commands on the terminal don't paste as the double quotes are wrong.'''
+
|| At the prompt
|| Let us see how grep works.
+
  
Let’s find students from the computer’s stream.
+
'''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.
 
|-  
 
|-  
|| Type
+
|| Type'''sed -n '3,6!p' seddemo.txt'''
  
'''grep "computers" grepdemo.txt '''press enterShow side by side '''grepdemo.txt '''file entries.
+
Press''' Enter'''
|| At the terminal type, '''grep space double quote computers double quote space grepdemo dot t x t '''and press '''Enter'''. The output shows entries from the computer’s stream.
+
  
Let us compare it with the original file.
+
'''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.
 
|-  
 
|-  
|| Highlight Zubin entry in the file
+
||  
|| Notice that the entry for Zubin is missing. This happens because '''grep''' is case sensitive.
+
|| So far, we have specified the lines in a file on which an action applies.
 +
 
 +
This method is known as '''line addressing.'''
 
|-  
 
|-  
|| Point to the capital C
+
|| '''Slide 7'''
|| Why is this entry missing?
+
  
'''grep''' searched for computers in lowercase.
+
'''Line addressing'''
 +
|| Line addressing identifies lines using their position in the file.
  
Zubin’s stream is written as Computers with a capital '''C'''.
+
It is one way to select specific lines for an action.
 +
 
 +
Another method is called '''context addressing'''.
 
|-  
 
|-  
|| Type '''grep -i "computers" grepdemo.txt '''Highlight '''-i'''press '''Enter'''
+
|| '''Slide 8'''
|| Pattern matching is case sensitive by default.To make it case insensitive, we need to use the '''hyphen i '''option with '''grep'''.
+
  
Type this command and press '''Enter'''.
+
'''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.
 
|-  
 
|-  
|| Point to the output.
+
||  
|| Now, all four matching entries are displayed.
+
|| Let us see an example.
 +
|-
 +
|| Highlight the lines with word computers in the text editor- show the text file '''seddemo.txt'''.
  
Observe that,''' grep''' lists only the lines that match a given pattern.
+
At the prompt'''sed -n '/[cC]omputers/p' seddemo.txt'''
  
We can also do the reverse.
+
Highlight '''-n'''
|-
+
|| Type '''grep -iv "pass" grepdemo.txt '''Highlight '''-v'''press '''Enter'''
+
  
Point to the output.
+
highlight '''/[cC]omputers/'''
|| Type this command. The '''hyphen v '''option lists the lines that do not match a pattern. Press '''Enter '''to see the output.
+
  
Output shows students who failed or have incomplete results.
+
'''highlight p'''
|-
+
|| Show side by side home folder.Type '''grep -iv "pass" grepdemo.txt > notpass.txt''' press '''Enter'''.
+
|| Let us store the output in a file named '''not pass dot t x t'''.
+
  
 +
Press Enter.
  
Type this command and press''' Enter'''.The file is created successfully.
+
highlight '''/[cC]omputers/'''
|-
+
|| Suppose we want to print the lines containing the word '''computers'''.
|| Type '''cat notpass.txt '''press '''Enter'''.
+
|| To view the students who have not passed, type '''cat space not pass dot t x t '''and press '''Enter'''.Observe the output.
+
|-
+
|| Type '''grep -i "fail" grepdemo.txt '''
+
  
 +
Go to the Terminal.
  
Highlight''' '''output
+
Type the following command.
|| Type this command to list the failed students and press '''Enter'''.The output shows all students who have failed.
+
|-
+
|| type
+
  
'''grep -in "fail" grepdemo.txt '''
+
'''hyphen n '''suppresses default output.
  
 +
This is the '''Context address'''.
  
press enter
+
It matches lines containing the word computers with small c and capital C.
|| Let us use the '''hyphen n '''option to show line numbers with matching entries.
+
  
Type this command and press '''Enter'''.
+
'''p''', prints the matched lines.
 +
 
 +
Press '''Enter'''.
  
 
Observe the output.
 
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 '''clear '''press''' Enter'''.
+
|| Type the command:
|| Let us clear the terminal screen.
+
|-
+
|  |
+
|| So far, we used single-word patterns. For multi-word patterns, we have to put the entire pattern in quotes.
+
|-
+
|| type
+
  
'''grep -i "ankit saraf" grepdemo.txt '''press enter
+
'''sed -n '/[cC]omputers/w computer-student' seddemo.txt'''
|| Type this command and press '''Enter'''.
+
  
We can see that '''Ankit Saraf''''s record is displayed.
+
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'''.
 
|-  
 
|-  
|| At the prompt type
+
|| Type
  
'''grep -i "fail" grepdemo.txt notpass.txt'''
+
'''cat computer-student '''andPress '''Enter'''
 +
|| Let us view the content of the '''computer hyphen student '''file.
  
press''' Enter'''.
+
type '''cat''' '''space''' '''computer hyphen student''' and press''' Enter'''.
|| We can also find patterns in multiple files.Type this command and press''' Enter'''.
+
  
Observe the output.
+
We see that lines that match the given pattern are in the file.
 
|-  
 
|-  
|| Point to the output.
+
|| Type the following command.'''sed -n -e '/electronics/w electro' -e '/civil/w civil' seddemo.txt'''Press '''Enter'''
|| While searching for multiple files, '''grep''' shows the filename for each match.
+
  
In this example, '''grep demo dot t x t '''and '''not pass dot t x t '''are searched.
+
'''Highlight -e'''
|-
+
 
|| Show only the inside files contents of '''notpass.txt '''and '''grepdemo.txt'''
+
'''Highlight /electronics/w electro'''
|| These are the records from '''not pass dot t x t''' file and these are the records from '''grep demo dot t x t''' file.
+
 
 +
'''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.
 
|-  
 
|-  
|| Type
+
|| '''cat electro '''Press Enter
  
'''grep -c “Fail” grepdemo.txt''' press '''Enter'''.
+
Highlight the contents
|| To display only the number of matches, use the '''hyphen c''' option.
+
  
Type this command and press the '''Enter '''key. Output shows the number of matches.
+
'''cat civil'''
  
|-
+
Press Enter
|| '''Slide 8'''
+
 
 +
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'''
 
'''Summary'''
  
 
In this tutorial, we have learnt to:
 
In this tutorial, we have learnt to:
* View the content of a file
+
* Use '''sed''' as a stream editor to process text
* List entries of a particular stream  
+
* Print specific lines from a file
* Ignore cases while searching
+
* Apply line-based addressing techniques
* Handle lines that do not match a pattern
+
* Use context-based addressing with pattern matching
* Display line numbers with the entries
+
* Redirect matched output to files
* Store the result in another file
+
* Count the number of matches
+
 
+
 
|| 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.
 
|-  
 
|-  
|| '''Slide 9'''
+
|| '''Slide 10'''
 
+
'''Assignment'''
+
  
As an assignment# Use grep to find all students in the “Maths” stream in grepdemo.txt.
+
'''Assignment '''
# List lines that do '''not''' contain civil and save to '''nonCivil.txt'''.
+
# Show line numbers of entries containing fail.
+
# Search for a multi-word pattern, for example Mani Chowdhari, in '''grepdemo.txt'''.
+
  
 +
As an assignment,
  
 +
Using the text file '''seddemo.txt''',
 +
# Print only the first five lines of the file.
 +
# Print all lines that do not contain the word "failed".
 +
# Write all lines containing the word student into a file named '''student-list.txt'''.
 
|| As an assignment, please do the following.
 
|| As an assignment, please do the following.
 
|-  
 
|-  
|| '''Slide 10 Thank you'''
+
|| '''Slide 11'''
  
 +
'''Thank you'''
  
 +
|| This Spoken Tutorial is brought to you by '''EduPyramids''' Educational Services Private Limited SINE IIT Bombay.
  
|| This Spoken Tutorial is brought to you by EduPyramids Educational Services Private Limited, SINE, IIT Bombay. Thank you.
+
Thank you.
 
|-
 
|-
 
|}
 
|}

Revision as of 13:26, 26 January 2026

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 of 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, Madhurig