Sed-Stream-Editor/C2/More-on-Sed-commands/English
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
|
Slide 3:
System requirements |
This tutorial is recorded using
|
Slide 4:
Prerequisites |
To follow this tutorial,you should know
If not, for relevant tutorials please visit our website. |
Slide:
Code files |
|
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 -f flag. |
> cat linux_doc.txt
|
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 -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 -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 -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
| |
Slide:
Assignment: > sed 's!half!1/2!' sample.txt |
As an Assignment,
|
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. |