Difference between revisions of "Sed-Stream-Editor/C2/More-on-Sed-commands/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " {|border=1 | align=center| '''Visual Cue''' | align=center| '''Narration''' |- || '''Slide 1:''' || Welcome to the spoken tutorial''' '''on '''More on''' '''Sed commands.'''...")
 
 
(One intermediate revision by the same user not shown)
Line 10: Line 10:
 
'''Learning Objectives'''
 
'''Learning Objectives'''
 
|| In this tutorial, we will learn to use
 
|| In this tutorial, we will learn to use
* e command
+
* '''e command'''
* -f flag, -e flag
+
* '''-f flag, -e flag'''
* Transform flag y and
+
* Transform '''flag y''' and
* Sed substitute command with
+
* '''Sed substitute command''' with
 
+
 
** Escaping character and
 
** Escaping character and
** Delimiter
+
** '''Delimiter'''
 
|-
 
|-
 
|| '''Slide 3:'''
 
|| '''Slide 3:'''
  
 
'''System requirements'''
 
'''System requirements'''
|| This tutorial is recorded using
+
|| This tutorial is recorded using '''Ubuntu Linux '''OS version 18.04  
* '''Ubuntu Linux '''OS version 18.04  
+
  
 
|-
 
|-
Line 29: Line 27:
 
'''Prerequisites'''
 
'''Prerequisites'''
 
|| To follow this tutorial,you should know  
 
|| To follow this tutorial,you should know  
* Basics of Linux.
+
basics of '''Linux'''.
  
 
If not, for relevant tutorials please visit our website.
 
If not, for relevant tutorials please visit our website.
Line 44: Line 42:
 
|| I have saved the code files required for this tutorial in the '''Downloads''' folder.
 
|| I have saved the code files required for this tutorial in the '''Downloads''' folder.
 
|-
 
|-
|| '''Open the terminal'''
+
|| Open the '''terminal'''
|| Open the terminal by pressing '''Ctrl+Alt+T''' keys simultaneously.
+
|| Open the '''terminal''' by pressing '''Ctrl+Alt+T''' keys simultaneously.
  
Press the '''Enter''' key after every command.
+
Press the '''Enter''' key after every '''command'''.
 
|-
 
|-
 
|| '''e option'''
 
|| '''e option'''
  
|| First we will see how to execute a set of '''Linux''' commands using sed.
+
|| First we will see how to execute a set of '''Linux commands''' using sed.
 
|-
 
|-
|| open the file '''command_exec.txt'''.
+
|| Open the file '''command_exec.txt'''.
  
 
> cd Downloads
 
> cd Downloads
Line 60: Line 58:
  
 
'''> sed ‘e’ command_exec.txt'''
 
'''> sed ‘e’ command_exec.txt'''
|| Let us open the file '''command_exec.txt''' which''' '''I have saved in the '''Downloads''' folder.
+
|| Let us open the file '''command_exec.txt''' which I have saved in the '''Downloads''' folder.
  
Here I have saved 4 unix commands in this''' '''file as shown.
+
Here I have saved 4 '''Unix commands''' in this file as shown.
  
We can execute all the unix commands in this file at one go.
+
We can execute all the '''Unix commands''' in this file at one go.
  
Type the execute command with''' ‘e’''' option as shown.
+
Type the '''execute command''' with''' ‘e’''' option as shown.
 
|-
 
|-
 
|| '''Highlight the output according to narration.'''
 
|| '''Highlight the output according to narration.'''
|| The output shows the present working directory, date, calendar and the username.
+
|| The output shows the '''present working directory''', date, calendar and the '''username'''.
 
|-
 
|-
 
|| '''-f flag'''
 
|| '''-f flag'''
|| Next we will see how to use multiple commands inside a script file with -'''f''' flag.
+
|| Next we will see how to use multiple ''commands'' inside a '''script file''' with '''hyphen f''' flag.
 
|-
 
|-
 
|| '''> cat linux_doc.txt'''
 
|| '''> cat linux_doc.txt'''
Line 82: Line 80:
 
In this file, let us change certain lines with multiple substitution.
 
In this file, let us change certain lines with multiple substitution.
  
For example, let us change all the '''linux''' words to '''GNU Linux''' and small u to capital U in the word '''Unix'''.
+
For example, let us change all the '''linux''' words to '''GNU Linux''' and small '''u''' to capital '''U''' in the word '''Unix'''.
  
 
Also we will change the lower case '''gnu''' to upper case '''GNU'''.
 
Also we will change the lower case '''gnu''' to upper case '''GNU'''.
Line 95: Line 93:
 
|| Let us open the file '''mycommands.txt.'''
 
|| Let us open the file '''mycommands.txt.'''
  
To perform multiple commands, enter the sed commands in a script file as shown.
+
To perform multiple '''commands''', enter the '''sed commands''' in a script file as shown.
  
Use the curly brackets to group commands as shown here.
+
Use the '''curly brackets''' to group '''commands''' as shown here.
  
All the required substitution commands are given here.
+
All the required substitution '''commands''' are given here.
  
1,$ represents the address where the changes have to be done.
+
1,$ represents the '''address''' where the changes have to be done.
  
This will apply the substitute command from the first line to the end of the file.
+
This will apply the substitute '''command''' from the first line to the end of the file.
  
 
Save the file.
 
Save the file.
 
|-
 
|-
 
||  
 
||  
|| Switch back to the terminal.
+
|| Switch back to the '''terminal'''.
 
|-
 
|-
 
|| '''> sed -f mycommands.txt linux_doc.txt'''
 
|| '''> sed -f mycommands.txt linux_doc.txt'''
Line 115: Line 113:
  
 
'''Highlight linux_doc.txt'''
 
'''Highlight linux_doc.txt'''
|| Type the command with''' -f''' flag as shown.
+
|| Type the '''command''' with''' hyphen f flag''' as shown.
  
Here''', mycommands.txt '''is the script file where the commands are saved.
+
Here''', mycommands.txt '''is the '''script file''' where the '''commands''' are saved.
  
 
'''linux_doc.txt''' is the file where the changes have to be done.
 
'''linux_doc.txt''' is the file where the changes have to be done.
  
Let us execute and see the output.
+
Let us '''execute''' and see the output.
 
|-
 
|-
 
|| '''highlight the changes'''
 
|| '''highlight the changes'''
|| In the output, we can see the '''linux '''word changed to '''GNU Linux''' and capital U in '''Unix''' word.
+
|| In the output, we can see the '''linux '''word changed to '''GNU Linux'''.
 +
 
 +
And capital '''U''' in '''Unix''' word.
  
 
Also we can see the lowercase '''gnu''' changed to uppercase '''GNU'''.
 
Also we can see the lowercase '''gnu''' changed to uppercase '''GNU'''.
 
|-
 
|-
 
|| '''Show the output'''
 
|| '''Show the output'''
|| In this way, we can do many replacements in a document or any source file in a single command.
+
|| In this way, we can do many replacements in a document or any source file in a single '''command'''.
 
|-
 
|-
 
|| '''-e flag'''
 
|| '''-e flag'''
  
|| Next we will see how to combine multiple sed commands in a single command.
+
|| Next we will see how to combine multiple '''sed commands''' in a single '''command'''.
 
|-
 
|-
 
|| '''> cat linux_doc.txt'''
 
|| '''> cat linux_doc.txt'''
Line 140: Line 140:
 
|| Let us open the file '''linux_doc.txt.'''
 
|| Let us open the file '''linux_doc.txt.'''
  
Here each sentence is considered as a line. Blank lines will also be considered as a line.
+
Here each sentence is considered as a line.
 +
Blank lines will also be considered as a line.
  
Type the command as shown. This will display the number of lines in '''linux_doc.txt'''
+
Type the '''command''' as shown.  
 +
This will display the number of lines in '''linux_doc.txt'''
  
 
There are 6 lines in this file.
 
There are 6 lines in this file.
 
|-
 
|-
 
||  
 
||  
|| Now we will use the''' -e''' flag to get different lines from the file.
+
|| Now we will use the''' hyphen e flag''' to get different lines from the file.
 
|-
 
|-
 
|| '''sed -n -e '1p’ -e '4,5p' linux_doc.txt'''
 
|| '''sed -n -e '1p’ -e '4,5p' linux_doc.txt'''
  
|| Type the command as shown.
+
|| Type the '''command''' as shown.
  
We have used the''' -e''' flag before each print sed command.
+
We have used the''' hyphen e flag''' before each '''print sed command.'''
 
|-
 
|-
 
||  
 
||  
|| This will display non consecutive line 1, 4 and 5 from '''linux_doc.txt'''
+
|| This will display non-consecutive line 1, 4 and 5 from '''linux_doc.txt'''
 
|-
 
|-
 
|| '''Y option'''
 
|| '''Y option'''
|| Next we will see the transform flag''' y''' command.
+
|| Next we will see the transform '''flag y command'''.
 
|-
 
|-
 
|| '''Sed ‘y/abcdefghijkl/ABCDEFGHIJKL/’'''
 
|| '''Sed ‘y/abcdefghijkl/ABCDEFGHIJKL/’'''
  
 
'''Linux is free and open source'''
 
'''Linux is free and open source'''
|| Type the command with''' y''' flag as shown and press enter.
+
|| Type the '''command''' with''' y flag''' as shown and press '''Enter'''.
  
 
Type the input as “''' Linux is free and open source”'''
 
Type the input as “''' Linux is free and open source”'''
Line 171: Line 173:
 
|| Check the output.
 
|| Check the output.
  
It replaces each single character with another single character given in the sed command.
+
It replaces each single character with another single character given in the '''sed command'''.
  
That is, it converts all lowercase letters to uppercase letters, like the '''Unix tr''' command.
+
That is, it converts all lowercase letters to uppercase letters, like the '''Unix tr command'''.
  
Press''' Ctrl+c''' to return to the command prompt.
+
Press''' Ctrl+C''' to return to the '''command prompt'''.
 
|-
 
|-
 
|| '''Sed ‘y/a-z/A-Z/’'''
 
|| '''Sed ‘y/a-z/A-Z/’'''
Line 182: Line 184:
  
 
'''AA-bb-cc-ZZ'''
 
'''AA-bb-cc-ZZ'''
|| Type the command as shown here.
+
|| Type the '''command''' as shown here.
  
 
Type the input as shown.
 
Type the input as shown.
  
Range of characters will not work in''' y''' command.
+
Range of characters will not work in''' y command'''.
  
It replaces a, hyphen and z only in uppercase.
+
It replaces '''a, hyphen''' and '''z''' only, in uppercase.
  
Press''' Ctrl+c''' to return to the command prompt.
+
Press''' Ctrl+C''' to return to the '''command prompt.'''
 
|-
 
|-
 
||  
 
||  
|| Next we will see some exception cases while using sed substitute command.
+
|| Next we will see some exception cases while using '''sed substitute command'''.
 
|-
 
|-
 
|| '''> cat sample.txt'''
 
|| '''> cat sample.txt'''
Line 203: Line 205:
  
 
'''Highlight can’t'''
 
'''Highlight can’t'''
|| Type the command as shown.
+
|| Type the '''command''' as shown.
  
This substitute command didn’t work and we couldn’t see any output.
+
This '''substitute command''' didn’t work and we couldn’t see any output.
  
We have single quotes in between can’t.  
+
We have single quotes in between '''can’t'''.  
  
This single quote creates the problem in execution of this command.
+
This single quote creates the problem in '''execution''' of this '''command'''.
  
 
Recall that it is a good practice to write sed commands using quotes.
 
Recall that it is a good practice to write sed commands using quotes.
Line 215: Line 217:
 
There are some exceptions which need to be looked at while using quotes.
 
There are some exceptions which need to be looked at while using quotes.
  
Press''' Ctrl+c''' to return to the command prompt.
+
Press''' Ctrl+C''' to return to the '''command prompt.'''
 
|-
 
|-
 
|| '''sed “s/can’t/can/” sample.txt'''
 
|| '''sed “s/can’t/can/” sample.txt'''
|| To make it working you can use double quotes.
+
|| To make it working, you can use double quotes.
  
 
Use double quotes as shown to see the expected output.
 
Use double quotes as shown to see the expected output.
Line 233: Line 235:
  
 
'''Highlight \$10'''
 
'''Highlight \$10'''
|| Next let us change the text hundred dollars to numerical form.
+
|| Next let us change the text '''hundred dollars''' to numerical form.
  
Type the command as shown.
+
Type the '''command''' as shown.
  
 
We didn’t get the expected result.
 
We didn’t get the expected result.
  
The shell meaning of the $ symbol is special inside double quotes.
+
The shell meaning of the '''$ symbol''' is special inside double quotes.
  
Sed command will treat '''$1 '''as a shell argument and preserve the spaces.
+
'''Sed command''' will treat '''$1 '''as a '''shell argument''' and preserve the '''spaces'''.
  
It replaces the string “'''hundred dollars” '''to '''00.'''
+
It replaces the '''string “hundred dollars” '''to '''00.'''
  
To make it functional, type the command as shown.
+
To make it '''functional''', type the '''command''' as shown.
  
We can use the escape character backslash as shown here.
+
We can use the '''escape character backslash''' as shown here.
  
This will disable the special meaning of '''$''' character of '''$1'''
+
This will disable the special meaning of '''$ character''' of '''$1'''
 
|-
 
|-
 
||  
 
||  
|| Let us see another way to replace hundred dollars in text with $100 in numbers.
+
|| Let us see another way to replace '''hundred dollars''' in text with '''$100''' in numbers.
 
|-
 
|-
 
|| '''sed 's/hundred dollars/$100/' sample.txt'''
 
|| '''sed 's/hundred dollars/$100/' sample.txt'''
|| Here, within single quotes, a shell special character lost its shell meaning or its identity.
+
|| Here, within single quotes, a '''shell special character''' lost its '''shell''' meaning or its identity.
  
Special characters are considered as a normal text in the search and replacement string.
+
Special characters are considered as a normal text in the search and replacement '''string'''.
 
|-
 
|-
 
||  
 
||  
|| Next we will see how to use a different delimiter in sed command.
+
|| Next we will see how to use a different '''delimiter''' in '''sed command'''.
 
|-
 
|-
 
|| Highlight the second line , the word half
 
|| Highlight the second line , the word half
  
 
'''sed 's#half#1/2#' sample.txt'''
 
'''sed 's#half#1/2#' sample.txt'''
|| In this example, we want to replace the word half into numerical form i.e ½ in the second line.
+
|| In this example, we want to replace the word '''half''' into numerical form i.e '''½''' in the second line.
  
Type the command as shown.
+
Type the '''command''' as shown.
  
Recall that forward slash (/) is the most commonly used delimiter in the sed commands.
+
Recall that '''forward slash (/)''' is the most commonly used '''delimiter''' in the '''sed commands'''.
  
Here, we have used forward slash(/) inside search and replacement string.
+
Here, we have used '''forward slash(/)''' inside search and replacement '''string'''.
  
It is a good practice to use a different symbol as a delimiter in this case.
+
It is a good practice to use a different symbol as a '''delimiter''' in this case.
  
In this command we have used hash(#) as the delimiter.
+
In this '''command''' we have used '''hash(#)''' as the '''delimiter'''.
  
However, sed can use any character as a delimiter.
+
However, '''sed''' can use any character as a '''delimiter'''.
 
|-
 
|-
 
|| Slide 8:
 
|| Slide 8:
Line 286: Line 288:
 
||  
 
||  
 
|| In this tutorial, we learnt
 
|| In this tutorial, we learnt
* e (execute) command
+
* '''e command'''
* -f flag, -e flag
+
* '''-f flag, -e flag'''
* Transform flag y and
+
* Transform '''flag y''' and
* Sed substitute command with
+
* '''Sed substitute command''' with
 
+
 
** Escaping character and
 
** Escaping character and
** Delimiter
+
** '''Delimiter'''
  
 
|-
 
|-
Line 300: Line 301:
  
 
> sed 's!half!1/2!' sample.txt
 
> sed 's!half!1/2!' sample.txt
|| As an Assignment,
+
|| As an assignment,
# Change the delimiter as shown below and see the output.
+
# Change the '''delimiter''' as shown below and see the output.
  
 
|-
 
|-

Latest revision as of 14:09, 1 July 2021

Visual Cue Narration
Slide 1: Welcome to the spoken tutorial on More on Sed commands.
Slide 2:

Learning Objectives

In this tutorial, we will learn to use
  • e command
  • -f flag, -e flag
  • Transform flag y and
  • Sed substitute command with
    • Escaping character and
    • Delimiter
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, for relevant tutorials please visit our website.

Slide:

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
Point to the files in the Downloads folder. I have saved the code files required for this tutorial in the Downloads folder.
Open the terminal Open the terminal by pressing Ctrl+Alt+T keys simultaneously.

Press the Enter key after every command.

e option First we will see how to execute a set of Linux commands using sed.
Open the file command_exec.txt.

> cd Downloads

> cat command_exec.txt

> sed ‘e’ command_exec.txt

Let us open the file command_exec.txt which I have saved in the Downloads folder.

Here I have saved 4 Unix commands in this file as shown.

We can execute all the Unix commands in this file at one go.

Type the execute command with ‘e’ option as shown.

Highlight the output according to narration. The output shows the present working directory, date, calendar and the username.
-f flag Next we will see how to use multiple commands inside a script file with hyphen f flag.
> cat linux_doc.txt


Highlight according to narration

Let us open the file linux_doc.txt.

In this file, let us change certain lines with multiple substitution.

For example, let us change all the linux words to GNU Linux and small u to capital U in the word Unix.

Also we will change the lower case gnu to upper case GNU.

> gedit mycommands.txt

Highlight { }

Highlight s/ commands

Highlight 1,$

Let us open the file mycommands.txt.

To perform multiple commands, enter the sed commands in a script file as shown.

Use the curly brackets to group commands as shown here.

All the required substitution commands are given here.

1,$ represents the address where the changes have to be done.

This will apply the substitute command from the first line to the end of the file.

Save the file.

Switch back to the terminal.
> sed -f mycommands.txt linux_doc.txt

Highlight mycommands.txt

Highlight linux_doc.txt

Type the command with hyphen f flag as shown.

Here, mycommands.txt is the script file where the commands are saved.

linux_doc.txt is the file where the changes have to be done.

Let us execute and see the output.

highlight the changes In the output, we can see the linux word changed to GNU Linux.

And capital U in Unix word.

Also we can see the lowercase gnu changed to uppercase GNU.

Show the output In this way, we can do many replacements in a document or any source file in a single command.
-e flag Next we will see how to combine multiple sed commands in a single command.
> cat linux_doc.txt

> sed -n '$=' linux_doc.txt

Let us open the file linux_doc.txt.

Here each sentence is considered as a line. Blank lines will also be considered as a line.

Type the command as shown. This will display the number of lines in linux_doc.txt

There are 6 lines in this file.

Now we will use the hyphen e flag to get different lines from the file.
sed -n -e '1p’ -e '4,5p' linux_doc.txt Type the command as shown.

We have used the hyphen e flag before each print sed command.

This will display non-consecutive line 1, 4 and 5 from linux_doc.txt
Y option Next we will see the transform flag y command.
Sed ‘y/abcdefghijkl/ABCDEFGHIJKL/’

Linux is free and open source

Type the command with y flag as shown and press Enter.

Type the input as “ Linux is free and open source”

LInux Is FrEE AnD opEn sourCE Check the output.

It replaces each single character with another single character given in the sed command.

That is, it converts all lowercase letters to uppercase letters, like the Unix tr command.

Press Ctrl+C to return to the command prompt.

Sed ‘y/a-z/A-Z/’

aa-bb-cc-zz

AA-bb-cc-ZZ

Type the command as shown here.

Type the input as shown.

Range of characters will not work in y command.

It replaces a, hyphen and z only, in uppercase.

Press Ctrl+C to return to the command prompt.

Next we will see some exception cases while using sed substitute command.
> cat sample.txt This is the text in the sample.txt file.

Let us try to replace the word can’t to can.

> sed ‘s/can’t/can/’ sample.txt

Highlight can’t

Type the command as shown.

This substitute command didn’t work and we couldn’t see any output.

We have single quotes in between can’t.

This single quote creates the problem in execution of this command.

Recall that it is a good practice to write sed commands using quotes.

There are some exceptions which need to be looked at while using quotes.

Press Ctrl+C to return to the command prompt.

sed “s/can’t/can/” sample.txt To make it working, you can use double quotes.

Use double quotes as shown to see the expected output.

Highlight hundred dollars in the output of prev cat command

> cat sample.txt > sed “s/hundred dollars/$100/” sample.txt Highlight $

Highlight 00

> sed “s/hundred dollars/\$100/” sample.txt

Highlight \$10

Next let us change the text hundred dollars to numerical form.

Type the command as shown.

We didn’t get the expected result.

The shell meaning of the $ symbol is special inside double quotes.

Sed command will treat $1 as a shell argument and preserve the spaces.

It replaces the string “hundred dollars” to 00.

To make it functional, type the command as shown.

We can use the escape character backslash as shown here.

This will disable the special meaning of $ character of $1

Let us see another way to replace hundred dollars in text with $100 in numbers.
sed 's/hundred dollars/$100/' sample.txt Here, within single quotes, a shell special character lost its shell meaning or its identity.

Special characters are considered as a normal text in the search and replacement string.

Next we will see how to use a different delimiter in sed command.
Highlight the second line , the word half

sed 's#half#1/2#' sample.txt

In this example, we want to replace the word half into numerical form i.e ½ in the second line.

Type the command as shown.

Recall that forward slash (/) is the most commonly used delimiter in the sed commands.

Here, we have used forward slash(/) inside search and replacement string.

It is a good practice to use a different symbol as a delimiter in this case.

In this command we have used hash(#) as the delimiter.

However, sed can use any character as a delimiter.

Slide 8:

Summary

With this we come to the end of this tutorial. Let us summarize.
In this tutorial, we learnt
  • e command
  • -f flag, -e flag
  • Transform flag y and
  • Sed substitute command with
    • Escaping character and
    • Delimiter
Slide:

Assignment:

> sed 's!half!1/2!' sample.txt

As an assignment,
  1. Change the delimiter as shown below 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:

Please post your timed queries on this forum.
Slide: Acknowledgement Spoken Tutorial project is funded by the Ministry of Education (MoE), Govt. of India.
This is Neha Solanki from the Spoken Tutorial project signing off.

Thanks for joining.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat