Difference between revisions of "Sed-Stream-Editor/C2/Sed-substitute-command/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 81: Line 81:
 
||  
 
||  
  
'''sed ‘s/Unix/Linux/’'''
+
'''sed ‘s/Unix/Linux/’''' >> Enter
 +
 
 +
 
 
'''Highlight according to narration'''
 
'''Highlight according to narration'''
  
 
||Type,
 
||Type,
  
'''sed space''' within single quotes '''s forward slash Unix forward slash Linux forward slash''' >> Enter
+
'''sed space''' within single quotes '''s forward slash Unix forward slash Linux forward slash'''
  
 
Here '''‘s’''' indicates the '''substitute''' option.
 
Here '''‘s’''' indicates the '''substitute''' option.
Line 115: Line 117:
 
||Press '''“Ctrl+D”''' keys to terminate the standard input.
 
||Press '''“Ctrl+D”''' keys to terminate the standard input.
 
|-
 
|-
|| '''sed ‘s/Unix/Linux/’'''
+
|| '''sed ‘s/Unix/Linux/’''' >> Enter
  
'''>“unix is the most powerful OS” '''
+
'''>“unix is the most powerful OS” ''' >> Enter
  
  
 
||Let us execute the same '''command''' again.
 
||Let us execute the same '''command''' again.
  
Type '''unix is the most powerful OS''' and press '''Enter'''.
+
Type '''unix is the most powerful OS'''.
  
 
Note that we have typed '''‘u’''' in lowercase in '''Unix'''.
 
Note that we have typed '''‘u’''' in lowercase in '''Unix'''.
Line 298: Line 300:
 
||Highlight the word '''Success''' in the output.
 
||Highlight the word '''Success''' in the output.
  
Also note that in certain lines it didn’t substitute with the word '''victory'''.
+
||Also note that in certain lines it didn’t substitute with the word '''victory'''.
  
 
This is because '''Success''' word has capital ‘S’.
 
This is because '''Success''' word has capital ‘S’.

Latest revision as of 13:52, 3 March 2021

VISUAL CUE NARRATION
Slide 1: Welcome to the spoken tutorial on Sed substitute command.
Slide 2:

Learning Objectives

In this tutorial, we will learn about:
  • Workflow of Sed command
  • Introduction to Sed command
  • Sed substitute 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 Linux spoken tutorials on this website.

Slide 5:

Code files

  • 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
Slide 4:Workflow of SED commands

1. Read - Sed reads a line from the input stream.

It can be a file or standard input.

It is stored in an internal buffer called pattern buffer.

Sed follows a simple workflow.

First Sed reads a line from the input stream.

It can be a file or standard input.

It is stored in an internal buffer called pattern buffer.

Slide 4(a):

2. Execute – All Sed commands are executed sequentially on the pattern buffer.

3. Display- Sends the modified content to the output stream.

The pattern buffer will be empty after sending the data.

Second step is Execute.

All Sed commands are executed sequentially on the pattern buffer.


Third step is Display.

It sends the modified content to the output stream.

The pattern buffer will be empty after sending the data.

Open the terminal Open the terminal by pressing Ctrl+Alt+T keys simultaneously.

Remember to press the Enter key after every command.

First let us see how to use the Sed command with standard input.

sed ‘s/Unix/Linux/’ >> Enter


Highlight according to narration

Type,

sed space within single quotes s forward slash Unix forward slash Linux forward slash

Here ‘s’ indicates the substitute option.

substitute command will replace Unix with Linux.

Highlight sed Sed command is mostly used to replace the text in a file.

Here we are using the Sed command without a file.

So it will process from standard input.

Type

"Unix is the most powerful OS" >> Enter

Highlight the output

Press Ctrl+d

Type Unix is the most powerful OS and press Enter

We can see the word Unix is replaced with Linux.

Press “Ctrl+D” keys Press “Ctrl+D” keys to terminate the standard input.
sed ‘s/Unix/Linux/’ >> Enter

>“unix is the most powerful OS” >> Enter


Let us execute the same command again.

Type unix is the most powerful OS.

Note that we have typed ‘u’ in lowercase in Unix.

Here the replacement didn’t take place.

Highlight "unix">> the small u in unix.


Press “Ctrl+D" keys

This is because sed commands are case-sensitive.

Press “Ctrl+D” keys to return back to the command prompt.

cd Downloads

../Downloads>cat message.txt

Highlight Downloads


Highlight message.txt

I have saved a file message.txt in my Downloads folder.

Go to the directory where you have saved the message.txt on your system.

Type cat space message.txt >> Enter Let us see the content of message.txt

Type cat space message.txt

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

Clear the screen.

>sed ‘’ message.txt >> Enter Let us read the content of the file message.txt using sed.

Type sed space a pair of single quotes space message.txt

Highlight according to narration Look at the output.

It prints the content of the entire file.

Highlight the pair of single quotes A pair of single quotes implies the sed option.

In our case there is no sed option.

So it prints the contents of the pattern buffer on the standard output.

Slide 6: Syntax:

sed [options]...[command] [File]

Example:

sed G message.txt

The general syntax for sed command is given here.

In this example, capital ‘G’ is the option to add line space to the file.

message.txt is the file which the sed command will work with.

If no filename is provided with sed command, then the command will work on standard input data.

> sed G message.txt

Highlight the output

Let us execute the command and check the output.

Type sed space G space message.txt

We can observe line space is added to the output.

Next let us see about the substitute command.
Slide 7:

Substitute command

syntax:

sed ‘s/oldstring/newstring/’ file

sed ‘s/oldstring/newstring/’ file > output.txt

Here ‘s’ specifies the substitute command.

forward slash (/) is to delimit the two arguments.

oldstring is the string to be searched from the file.

newstring is the string to be replaced by the searched string.

file is the input text file.

The redirection symbol and a file name is used to redirect the output to another file.

By default sed outputs everything to standard output instead of saving it in a file.

Switch back to terminal Switch back to terminal.
>sed s/success/victory/ message.txt

Highlight where the replacement has not taken place

Type

sed space s forward slash success forward slash victory forward slash space message.txt

This will replace the word success with the word victory in message.txt.

We can use sed commands without using single quotes.

Highlight the word victory in the output. Note that the word success has been replaced with the word victory in a few places.
Highlight the word success in the output. I’ll explain later why the replacement didn’t take place in the entire file.

>sed s/success/great victory/ message.txt

Now let us try this command with space in the string.

Here we got an error and we didn’t get the expected output.

>sed ‘s/success/great victory/’ message.txt

Highlight great victory

Try the same command with single quotes as shown.

So quotes are needed when we use space or a special shell character inside the command.

It is good practice to write sed commands using quotes.

Highlight according to narration

>sed ‘s/success/victory/’ message.txt

Let us see the various options with the substitute command.

Type the command as shown.

Highlight the word victory in the output.


Note that this has changed success to victory once on each line.

Sed replaces only the first match in each line with the substitute command.

Highlight the word success in the output.


The last sentence has success twice. But it has changed only at one place.

It will not replace the second or third occurrence in the same line.

Highlight the word Success in the output. Also note that in certain lines it didn’t substitute with the word victory.

This is because Success word has capital ‘S’.

As we know, sed commands are case sensitive.

>sed ‘s/success/victory/ I’ message.txt Type the command as shown.

We can use the flag capital I to make the pattern match case insensitive.

Look at the output.

>sed ‘s/success/victory/ 2’ message.txt

Highlight the last sentence

If you want to replace the 2nd occurrence in a line, use 2 as a flag as shown.

>sed ‘s/success/victory/ 2’ message.txt

>sed ‘s/success/victory/’ message.txt What if we want to replace all the instances of matches in a file?

Use the ‘g’ option to represent global replacement.

>sed ‘s/success/victory/ Ig’ message.txt Next we will see how to replace from nth occurrence to all the occurrences in a line.

Type the command as shown.

Here, we have used the capital I and g flags together.

This will replace all the patterns globally with case insensitive.

> cat message.txt

Highlight ‘Albert’ at two places

>sed '/Albert/s/success/victory/' message.txt

Let us see another example.

We want to replace the word success with victory in the line which contains the text ‘Albert’.

Type the command as shown.

Let us check the output.

Let us see how the regular expression is used in substitute command.
> sed ‘s/[0-9]*//’ message.txt

Highlight the output

Type,

sed within single quotes s forward slash within square brackets 0 hyphen 9 asterisk forward slash forward slash space message.txt

Here regular expression represents one or more occurrences of 0-9 characters.

I want to replace it with the empty string.

In the output we can see, all the line numbers are replaced with empty spaces.

> sed ‘1,2 s/success/victory/g’ message.txt We can also perform the substitute command on specific lines.

Type the command as shown.

We can see the replacement in the first two lines

> sed ‘4,$ s/success/victory/g’ message.txt In this command, $ represents the last line.

Here, replacement takes place from the fourth line to the last line.

>> sed ‘/^2./ s/success/victory/’ message.txt We can select a particular line with the regular expressions to perform the substitute command.

For example, in this command caret (^)symbol represents the beginning of the line.

If the line begins with 2 dot, then on that line it will perform the substitute command.

With this we come to the end of this tutorial. Let us summarize.

Slide 8:

Summary

In this tutorial, we learnt
  • Work flow of Sed command
  • Introduction to Sed command
  • Sed substitute command
Slide:9

Assignment

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

  1. sed 's/[0-9.]*/(*)/' message.txt
  2. sed '/secrets/s/success/victory/g' message.txt
  3. sed 's/-/:/' message.txt
As an assignment, try the below commands and see the output.
Slide 10:

(About Spoken Tutorial Project)

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

Please download and watch it.

Slide 11:

(About Spoken Tutorial Project)

The Spoken Tutorial Project Team conducts workshops and gives certificates.

For more details, please write to us.

Slide

Forum questions:

  • Do you have questions in THIS Spoken Tutorial?
  • Please visit this site
  • Choose the minute and second where you have the question
  • Explain your question briefly
  • The Spoken Tutorial project team will ensure an answer
  • You will have to register on this website to ask questions.
Slide: Forum for specific  Questions
  • The Spoken Tutorial forum is for specific questions on this tutorial.
  • Please do not post unrelated and general questions on them.
  • This will help reduce the clutter.
  • With less clutter, we can use these discussions as instructional material.
Slide: Acknowledgement Spoken Tutorial project is funded by the Ministry of Education (MoE), Govt. of India.
This is Pooja from Spoken Tutorial Project, IIT Bombay signing off.

Thanks for joining.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat