Sed-Stream-Editor/C2/Sed-commands-with-Files/English

From Script | Spoken-Tutorial
Revision as of 14:36, 15 March 2021 by Nancyvarkey (Talk | contribs)

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


VISUAL CUE NARRATION
Slide 1: Welcome to the spoken tutorial on Sed Commands with Files.
Slide 2:

Learning Objectives

In this tutorial, we will learn about
  • Delete command
  • Write command
  • Read command
  • Append command
  • Insert command
  • Change command
  • Execute command
Slide 3:

System requirements

This tutorial is recorded using Ubuntu Linux OS version 18.04
Slide 4:

Prerequisites

To follow this tutorial, you should know basics of Linux.

If not, then go through the relevant spoken tutorials on this website.

Slide 5(a):

Delete Command

First we will see about the delete command in sed
  • Sed command can be used to remove specific lines which matches a given pattern in the file.
  • This command removes lines only from the pattern buffer.
  • The original file remains unchanged.
Slide 5(b):

Syntax: sed [range1][range2]d filename Highlight according to narration

This is the syntax for the delete command.
  • range1, range2 are the starting and ending range.
  • It can be either a line number or pattern strings.
  • d specifies the delete option
Slide 6:

Code files - IndianBooks.txt

  • The files used in this tutorial are available in the Code Files link on this tutorial page.
  • Please download and extract them
  • Make a copy and then use them while practising
Open the terminal Open the terminal by pressing Ctrl+Alt+T keys simultaneously.

Press the Enter key after every command.

>> cat IndianBooks.txt Let us open the file IndianBooks.txt which I have saved in the Downloads folder.

I’ll be using this file for demonstration of various file commands.

> sed ‘d’ IndianBooks.txt

First we will see how the delete command is used in files.

Type the command as shown.

We couldn’t see any output. Why?

We didn’t give any line address. So Sed operates on every line by default.

It deletes all the lines from the pattern buffer.

Hence it does not print anything on the standard output.

>sed ‘4d’ IndianBooks.txt

Point to the output

Let us give the specific line number as shown for the Sed command to work.

We can see the 4th line is deleted.

sed ‘2,6d’ IndianBooks.txt Type the command as shown.

We can also give a range of line numbers that can be deleted.

This command deletes all the lines from 2 to 6.

sed ‘/R K Narayan/d’ IndianBooks.txt We can specify patterns instead of line numbers as shown here.

This has deleted the lines which have the author name as R K Narayan.

> cat IndianBooks.txt

Highlight the text between Malgudi and Crescent.

>sed ‘/Malgudi/,/Crescent/d’ IndianBooks.txt

We can also specify a range using a textual pattern.

For example, we want to remove all the lines between the patterns Malgudi and Crescent.

Type sed space within single quotes forward slash Malgudi forward slash comma forward slash Crescent forward slash d space IndianBooks.txt

Look at the output.

>sed ‘$d’ IndianBooks.txt

Highlight $d

Type the command as shown.

We can delete the last line of a file using the special character dollar $.

>sed ‘1!d’ IndianBooks.txt

Highlight 1d

Next we will see how to use the negation operator.

Type the command as shown.

Here ‘1’ specifies the line number of the file.

The negation operator with d indicates, not to delete the first line.

We can see this sed command removes all the lines except the first line.

> sed ‘1d;$d’ IndianBooks.txt This command will delete the first and last lines.

We can specify the list of lines to be removed with semicolon as a delimiter.

sed ‘/^1/d’ IndianBooks.txt This command removes all the lines that begin with ‘1’.

Recall that the caret(^) symbol specifies the starting of a line.

We can see that line 1 and line 10 are deleted.

>sed ‘/3$/d’ <IndianBooks.txt Type the command as shown.

This command deletes all the lines that end with '3'.

$ dollar is to indicate the end of the line.

We can see that the lines ending with 3 in the published year are deleted.

>sed '/[38]$/d' IndianBooks.txt This sed command deletes all the lines that end with either '3' or '8'.

Note that the lines ending with '3' or '8' in the published year are deleted.

Slide 7: Delete command: Note: In all the above examples, the sed command does not remove the lines from the source file.

It prints the content of the file on the Linux terminal by removing the lines.

To remove the lines from the source file itself, use hyphen i option with sed command.

Otherwise, you can redirect the output of the sed command to another file.

Next we will see about the write command.
Slide 8:

Write command

Sed reads a line and places it in a pattern buffer.

Then writes the pattern buffer to the given output file according to the given options.

Slide 9:

Write command

Syntax: sed 'ADDRESS w outputfile' inputfile

Here ‘w’ represents the write command. It should be at the end of all options.

Address can be either line numbers or pattern strings.

outputfile is the filename where the output has to be stored.

inputfile is the input file for the command to execute.

Note that sed creates an output file if it is not present and over writes if it is already present.

Switch back to the terminal.
sed -n ‘w books_copy.txt’ IndianBooks.txt Type the write command as shown:

If you open books_copy.txt, you will see the copy of the content of IndianBooks.txt

no highlight

> sed -n ‘1~2 w out.txt’ IndianBooks.txt

We can copy only certain line numbers to another file.

Type the command to copy all odd lines to another file.

We can see only the odd lines are copied to out.txt

sed -n ‘/Gandhi/ w GandhiBooks.txt’ IndianBooks.txt Type the command as shown.

We can give patterns that have to be copied to another file.

This command will store the books with author name Gandhi to another file.

Let us see the output file GandhiBooks.txt

Next we will see the read command.
Slide 10:

Read Command

Using the read command, we can read the content of another file.

It appends with each line of the input file as specified in the sed command pattern.

Switch back to the terminal.
> cat new_books.txt Let us open the file new_books.txt which has 3 lines.
sed ‘4 r< new_books.txt’ IndianBooks.txt This command instructs sed to read the content from new_book.txt.

Then it inserts after the 4th line in IndianBooks.txt

Here 4 specifies the line number and r specifies the read command.

Slide 11:

Assignment

As an assignment, try the below commands and see the output.

sed ‘4,6 r new_books.txt’ IndianBooks.txt

Append command Next we will see about the append command.

> sed ‘$ a 11. Broken wings, Sarojini Naidu, 1916’ IndianBooks.txt

Let us append a new book entry at the end of the IndianBooks.txt file.

Type the command with the new book entry as shown.

Here a indicates append command and $ dollar indicates the end of the file.

>sed ‘6 a 11. Broken wings, Sarojini Naidu, 1916’ IndianBooks.txt Suppose if we want to insert in between the lines.

Type the command with line number as shown.

Here '6' indicates the line number and a is the append command.

>sed ‘/Hind Swaraj/ a 11. Broken wings, Sarojini Naidu, 1916’ IndianBooks.txt

Highlight /Hind Swaraj/

This command specifies a text pattern instead of line number.

Look at the output.

The new book entry has appended after the pattern ‘Hind swaraj’.

> sed ‘/My/ a 11. Broken wings, Sarojini Naidu, 1916’ IndianBooks.txt

Highlight /My/

Type this command:

If there are multiple patterns matching, then the text is appended after each match.

Next we will see the insert command.
Insert command

> sed ‘3 i 3. Our films, Their Films, Satyajit Ray, 1976’ IndianBooks.txt

Highlight 3 and ‘i’

Insert command is the same as append command.

But it inserts a line before a specific position.

Type the command as shown:

This command inserts the new text before the third line.

Here 3 is the line number and ‘i’ represents the insert command.

change command Next we will see about the change or replace command.

This command replaces the existing line with new text.

> sed ‘3 c 3. Our films, Their Films, Satyajit Ray, 1976’ IndianBooks.txt Let us replace the third line in IndianBooks.txt with some new text.

Type the command as shown.

‘c’ indicates the change command.

We can see the third line is replaced with the new text.

> sed ‘6, 8 c 3. Our films, Their Films, Satyajit Ray, 1976’ IndianBooks.txt We can also replace multiple lines with a single line.

Type the command as shown.

The lines '6' to '8' are replaced with the new text.

Next we will see the execute command.
Execute command

>> sed ‘4 e date’ IndianBooks.txt

Type the command as shown.

e represent the execute command.

This will execute the external command and insert at the matched pattern.

We can see the date is inserted at the fourth line.

Slide 12:

Summary

With this we come to the end of this tutorial. Let us summarize.
In this tutorial, we learnt
  • Delete command
  • Write command
  • Read command
  • Append command
  • Insert command
  • Change command
  • Execute command
Slide 13:

Assignment:

As an assignment, try the below commands and see the output.

> sed ‘4,6 r date’ IndianBooks.txt

> sed ‘/My/ e date’ IndianBooks.txt

Slide 14:

(About Spoken Tutorial Project)

The video at the following link, summarizes the Spoken Tutorial project.

Please download and watch it.

Slide 15:


(About Spoken Tutorial Project)

The Spoken Tutorial Project Team conducts workshops and gives certificates.

For more details, please write to us.

Slide 16:

Forum questions:

Please post your timed queries on this forum.
Slide 17:

Acknowledgement

Spoken Tutorial project is funded by the Ministry of Education (MoE), Govt. of India.
This is Pooja from Spoken Tutorial project signing off.

Thanks for joining.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat