Difference between revisions of "Sed-Stream-Editor/C2/Sed-substitute-command/English"
(Created page with " {| border="1" |- || '''VISUAL CUE''' || '''NARRATION''' |- || '''Slide 1:''' || Welcome to the '''spoken tutorial '''on '''Sed substitute''' command. |- || '''Slide 2:''' ''...") |
Nancyvarkey (Talk | contribs) |
||
Line 6: | Line 6: | ||
|- | |- | ||
|| '''Slide 1:''' | || '''Slide 1:''' | ||
− | || Welcome to the '''spoken tutorial '''on '''Sed substitute''' | + | || Welcome to the '''spoken tutorial '''on '''Sed substitute command'''. |
|- | |- | ||
|| '''Slide 2:''' | || '''Slide 2:''' | ||
Line 12: | Line 12: | ||
'''Learning Objectives''' | '''Learning Objectives''' | ||
|| In this tutorial, we will learn about: | || In this tutorial, we will learn about: | ||
− | * Workflow of '''Sed''' | + | * Workflow of '''Sed command''' |
− | * Introduction to '''Sed''' | + | * Introduction to '''Sed command''' |
− | * '''Sed''' '''substitute''' | + | * '''Sed''' '''substitute command''' |
|- | |- | ||
|| '''Slide 3:''' | || '''Slide 3:''' | ||
'''System requirements''' | '''System requirements''' | ||
− | || This tutorial is recorded using | + | || This tutorial is recorded using '''Ubuntu Linux '''OS version 18.04 |
− | + | ||
|- | |- | ||
|| '''Slide 4:''' | || '''Slide 4:''' | ||
'''Prerequisites''' | '''Prerequisites''' | ||
− | ||To follow this tutorial,you should know | + | ||To follow this tutorial, you should know basics of '''Linux'''. |
− | + | ||
− | If not, then go through the Linux spoken tutorials on this website. | + | If not, then go through the '''Linux spoken tutorials''' on this website. |
|- | |- | ||
|| Slide 5: | || Slide 5: | ||
Line 45: | Line 43: | ||
It can be a file or standard input. | It can be a file or standard input. | ||
− | It is stored in an internal buffer called '''pattern buffer'''. | + | It is stored in an internal '''buffer''' called '''pattern buffer'''. |
− | || '''Sed''' follows a simple workflow | + | || '''Sed''' follows a simple workflow. |
First''' Sed''' reads a line from the input stream. | First''' Sed''' reads a line from the input stream. | ||
Line 52: | Line 50: | ||
It can be a file or standard input. | It can be a file or standard input. | ||
− | It is stored in an internal buffer called '''pattern buffer'''. | + | It is stored in an internal '''buffer''' called '''pattern buffer'''. |
|- | |- | ||
|| '''Slide 4(a):''' | || '''Slide 4(a):''' | ||
− | 2. '''Execute''' – All '''Sed''' | + | 2. '''Execute''' – All '''Sed commands''' are executed sequentially on the pattern buffer. |
3. '''Display-''' Sends the modified content to the output stream. | 3. '''Display-''' Sends the modified content to the output stream. | ||
Line 64: | Line 62: | ||
|| Second step is '''Execute'''. | || Second step is '''Execute'''. | ||
− | All '''Sed''' | + | All '''Sed commands''' are executed sequentially on the '''pattern buffer'''. |
+ | |||
Third step is '''Display.''' | Third step is '''Display.''' | ||
Line 70: | Line 69: | ||
It sends the modified content to the output stream. | It sends the modified content to the output stream. | ||
− | The pattern buffer will be empty after sending the data. | + | The '''pattern buffer''' will be empty after sending the '''data'''. |
|- | |- | ||
|| 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. |
− | Remember to press the '''Enter''' | + | Remember to press the '''Enter''' key after every '''command'''. |
|- | |- | ||
|| | || | ||
− | || First let us see how to use the '''Sed''' | + | || First let us see how to use the '''Sed command''' with standard input. |
|- | |- | ||
|| | || | ||
Line 85: | Line 84: | ||
'''Highlight according to narration''' | '''Highlight according to narration''' | ||
− | Type | + | ||Type, |
− | Unix | + | '''sed space''' within single quotes '''s forward slash Unix forward slash Linux forward slash''' >> Enter |
− | + | 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''' | |
− | Type '''Unix is the most powerful OS '''and press Enter | + | |
− | We can see the '''Unix''' | + | We can see the word '''Unix''' is replaced with '''Linux'''. |
− | Press “Ctrl+ | + | |- |
+ | ||Press '''“Ctrl+D”''' keys | ||
+ | ||Press '''“Ctrl+D”''' keys to terminate the standard input. | ||
|- | |- | ||
|| '''sed ‘s/Unix/Linux/’''' | || '''sed ‘s/Unix/Linux/’''' | ||
Line 116: | Line 119: | ||
'''>“unix is the most powerful OS” ''' | '''>“unix is the most powerful OS” ''' | ||
− | |||
+ | ||Let us execute the same '''command''' again. | ||
− | + | Type '''unix is the most powerful OS''' and press '''Enter'''. | |
− | + | ||
− | + | Note that we have typed '''‘u’''' in lowercase in '''Unix'''. | |
− | + | ||
− | Note that we have typed ‘u’ in | + | |
Here the replacement didn’t take place. | Here the replacement didn’t take place. | ||
− | + | |- | |
+ | ||Highlight '''"unix"'''>> the small u in unix. | ||
− | Press “Ctrl+ | + | |
+ | 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''' | || '''cd Downloads''' | ||
Line 140: | Line 145: | ||
'''Highlight message.txt''' | '''Highlight message.txt''' | ||
− | || I have saved a file '''message.txt '''in my | + | || I have saved a file '''message.txt '''in my '''Downloads''' folder. |
− | Go to the directory where you have | + | Go to the directory where you have saved the '''message.txt''' on your system. |
− | Let us see the content of '''message.txt ''' | + | |- |
+ | ||Type '''cat space message.txt ''' >> '''Enter''' | ||
+ | ||Let us see the content of '''message.txt ''' | ||
Type '''cat space message.txt ''' | Type '''cat space message.txt ''' | ||
− | I’ll be using this file for demonstration of sed commands. | + | I’ll be using this file for demonstration of '''sed commands'''. |
Clear the screen. | Clear the screen. | ||
|- | |- | ||
− | || '''>sed ‘’ message.txt''' | + | || '''>sed ‘’ message.txt''' >> '''Enter''' |
− | || Let us read the content of the file '''message.txt '''using ''' | + | || Let us read the content of the file '''message.txt '''using '''sed'''. |
− | Type '''sed space a pair of single quotes space message.txt''' | + | Type '''sed space''' a pair of single quotes '''space message.txt''' |
|- | |- | ||
|| Highlight according to narration | || Highlight according to narration | ||
Line 162: | Line 169: | ||
It prints the content of the entire file. | 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. | |
− | + | ||
− | In our case there is no ''' | + | |
− | So it prints the contents of the pattern buffer on the standard output. | + | So it prints the contents of the '''pattern buffer''' on the standard output. |
|- | |- | ||
||'''Slide 6: Syntax:''' | ||'''Slide 6: Syntax:''' | ||
Line 176: | Line 184: | ||
'''sed G message.txt''' | '''sed G message.txt''' | ||
− | || The general syntax for ''' | + | || The general syntax for '''sed command''' is given here. |
− | In this example, capital ‘'''G’ '''is the option to add line space to the file. | + | In this example, capital ‘'''G’ '''is the option to add '''line space''' to the file. |
− | '''message.txt '''is the file where the sed command will work with. | + | '''message.txt '''is the file where the '''sed command''' will work with. |
− | If no filename is provided with ''' | + | If no filename is provided with '''sed command''', then the '''command''' will work on standard input data. |
|- | |- | ||
|| | || | ||
Line 188: | Line 196: | ||
> '''sed G message.txt''' | > '''sed G message.txt''' | ||
− | + | Highlight the output | |
− | ||Let us execute the command and check the output. | + | ||Let us execute the '''command''' and check the output. |
Type '''sed space G space message.txt ''' | Type '''sed space G space message.txt ''' | ||
− | We can observe line space is added to the output. | + | We can observe '''line space''' is added to the output. |
|- | |- | ||
|| | || | ||
− | || Next let us see about the substitute command. | + | || Next let us see about the '''substitute command'''. |
|- | |- | ||
|| '''Slide 7:''' | || '''Slide 7:''' | ||
Line 209: | Line 217: | ||
|| | || | ||
− | Here ‘s’ specifies the substitute command. | + | Here '''‘s’''' specifies the '''substitute command'''. |
− | forward slash (/) is to delimit the two arguments. | + | '''forward slash (/)''' is to '''delimit''' the two '''arguments'''. |
− | '''oldstring''' is the string to be searched from the file. | + | '''oldstring''' is the '''string''' to be searched from the file. |
− | '''newstring''' is the string to be replaced by the searched string. | + | '''newstring''' is the '''string''' to be replaced by the searched '''string'''. |
'''file''' is the input text file. | '''file''' is the input text file. | ||
− | The redirection symbol and a file name is used to redirect the output to another 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. | + | By default '''sed''' outputs everything to standard output instead of saving it in a file. |
|- | |- | ||
− | || | + | || Switch back to '''terminal''' |
− | || Switch back to terminal. | + | || Switch back to '''terminal'''. |
|- | |- | ||
||'''>sed s/success/victory/ message.txt''' | ||'''>sed s/success/victory/ message.txt''' | ||
Line 233: | Line 241: | ||
'''sed space s forward slash success forward slash victory forward slash space message.txt''' | '''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 | + | This will replace the word '''success''' with the word '''victory''' in '''message.txt'''. |
− | We can use '''sed''' | + | We can use '''sed commands''' without using single quotes. |
− | Note that the word '''success''' has been replaced with the word '''victory''' in a few places. | + | |- |
+ | ||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. | ||
− | |||
|- | |- | ||
|| | || | ||
Line 245: | Line 258: | ||
'''>sed s/success/great victory/ message.txt''' | '''>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. | Here we got an error and we didn’t get the expected output. | ||
+ | |- | ||
+ | ||'''>sed ‘s/success/great victory/’ message.txt''' | ||
− | Try the same command with single quotes as shown. | + | '''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. | + | 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. | + | It is good practice to write '''sed commands''' using quotes. |
|- | |- | ||
||Highlight according to narration | ||Highlight according to narration | ||
>'''sed ‘s/success/victory/’ message.txt''' | >'''sed ‘s/success/victory/’ message.txt''' | ||
− | || Let us see the various options with the substitute command. | + | || Let us see the various options with the '''substitute command'''. |
− | Type the command as shown. | + | Type the '''command''' as shown. |
− | + | |- | |
+ | ||Highlight the word '''victory''' in the output. | ||
− | |||
− | The last sentence has '''success''' twice. But it has changed only at one place. | + | ||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. | It will not replace the second or third occurrence in the same line. | ||
− | Also note that in certain lines it didn’t substitute with the word victory. | + | |- |
+ | ||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’. | This is because '''Success''' word has capital ‘S’. | ||
− | As we know, '''sed''' | + | As we know, '''sed commands''' are case sensitive. |
|- | |- | ||
|| >'''sed ‘s/success/victory/ I’ message.txt''' | || >'''sed ‘s/success/victory/ I’ message.txt''' | ||
− | || Type the command as shown. | + | || Type the '''command''' as shown. |
− | We can use the flag capital '''I''' to make the pattern match case insensitive. | + | We can use the '''flag''' capital '''I''' to make the '''pattern''' match case '''insensitive'''. |
Look at the output. | Look at the output. | ||
Line 290: | Line 314: | ||
Highlight the last sentence | Highlight the last sentence | ||
− | || If you want to replace the 2nd occurrence in a line, use 2 as a flag as shown. | + | || 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/ 2’ message.txt''' | ||
Line 297: | Line 321: | ||
|| What if we want to replace all the instances of matches in a file? | || What if we want to replace all the instances of matches in a file? | ||
− | Use the ‘g’ option to represent global replacement. | + | Use the '''‘g’''' option to represent '''global''' replacement. |
|- | |- | ||
|| '''>sed ‘s/success/victory/ Ig’ message.txt''' | || '''>sed ‘s/success/victory/ Ig’ message.txt''' | ||
|| Next we will see how to replace from nth occurrence to all the occurrences in a line. | || Next we will see how to replace from nth occurrence to all the occurrences in a line. | ||
− | Type the command as shown. | + | Type the '''command''' as shown. |
− | Here, we have used the capital '''I''' and '''g''' | + | Here, we have used the capital '''I''' and '''g flag'''s together. |
− | This will replace all the patterns globally with case insensitive. | + | This will replace all the '''patterns globally''' with case '''insensitive'''. |
|- | |- | ||
|| '''> cat message.txt''' | || '''> cat message.txt''' | ||
Line 316: | Line 340: | ||
|| Let us see another example. | || Let us see another example. | ||
− | We want to replace the word success with victory in the line which contains the text '''‘Albert’'''. | + | We want to replace the word '''success''' with '''victory''' in the line which contains the text '''‘Albert’'''. |
− | Type the command as shown. | + | Type the '''command''' as shown. |
Let us check the output. | Let us check the output. | ||
|- | |- | ||
|| | || | ||
− | || Let us see how the regular expression is used in substitute command. | + | || Let us see how the regular expression is used in '''substitute command'''. |
|- | |- | ||
||'''> sed ‘s/[0-9]*//’ message.txt''' | ||'''> sed ‘s/[0-9]*//’ message.txt''' | ||
Line 330: | Line 354: | ||
|| Type, | || Type, | ||
− | '''sed within single quotes s forward slash within square brackets 0 hyphen 9 asterisk forward slash forward slash space message.txt''' | + | '''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. | + | Here regular expression represents one or more occurrences of '''0-9 characters'''. |
− | I want to replace it with the empty string. | + | I want to replace it with the empty '''string'''. |
In the output we can see, all the line numbers are replaced with empty spaces. | In the output we can see, all the line numbers are replaced with empty spaces. | ||
|- | |- | ||
|| '''> sed ‘1,2 s/success/victory/g’ message.txt''' | || '''> sed ‘1,2 s/success/victory/g’ message.txt''' | ||
− | || We can also perform the substitute command on specific lines. | + | || We can also perform the '''substitute command''' on specific lines. |
− | Type the command as shown. | + | Type the '''command''' as shown. |
We can see the replacement in the first two lines | We can see the replacement in the first two lines | ||
|- | |- | ||
|| '''> sed ‘4,$ s/success/victory/g’ message.txt''' | || '''> sed ‘4,$ s/success/victory/g’ message.txt''' | ||
− | ||In this command, $ represents the last line. | + | ||In this '''command, $''' represents the last line. |
Here, replacement takes place from the fourth line to the last line. | Here, replacement takes place from the fourth line to the last line. | ||
|- | |- | ||
|| '''>> sed ‘/^2./ s/success/victory/’ message.txt''' | || '''>> sed ‘/^2./ s/success/victory/’ message.txt''' | ||
− | ||We can select a particular line with the regular expressions to perform the substitute command. | + | ||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. | + | 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. | + | If the line begins with '''2 dot''', then on that line it will perform the '''substitute command'''. |
|- | |- | ||
|| | || | ||
Line 366: | Line 390: | ||
Summary | Summary | ||
|| In this tutorial, we learnt | || In this tutorial, we learnt | ||
− | * Work flow of '''Sed''' | + | * Work flow of '''Sed command''' |
− | * Introduction to '''Sed''' | + | * Introduction to '''Sed command''' |
− | * '''Sed | + | * '''Sed substitute command ''' |
|- | |- | ||
− | || Slide:9 | + | || Slide:9 |
Assignment | Assignment | ||
− | |||
− | # sed 's/[0-9.]*/(*)/' message.txt | + | As an assignment, try the below '''commands''' and see the output. |
− | # sed '/secrets/s/success/victory/g' message.txt | + | |
− | # sed 's/-/:/' message.txt | + | # '''sed 's/[0-9.]*/(*)/' message.txt''' |
+ | # '''sed '/secrets/s/success/victory/g' message.txt''' | ||
+ | # '''sed 's/-/:/' message.txt''' | ||
+ | |||
+ | || As an assignment, try the below '''commands''' and see the output. | ||
|- | |- | ||
Line 408: | Line 435: | ||
|| '''Slide: '''Forum for specific Questions | || '''Slide: '''Forum for specific Questions | ||
− | || * The Spoken Tutorial forum is for specific questions on this tutorial. | + | || |
+ | * The Spoken Tutorial forum is for specific questions on this tutorial. | ||
* Please do not post unrelated and general questions on them. | * Please do not post unrelated and general questions on them. | ||
* This will help reduce the clutter. | * This will help reduce the clutter. |
Revision as of 13:25, 1 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:
|
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 |
|
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.
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/’ Highlight according to narration |
Type,
sed space within single quotes s forward slash Unix forward slash Linux forward slash >> Enter 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/’
>“unix is the most powerful OS”
|
Let us execute the same command again.
Type unix is the most powerful OS and press Enter. Note that we have typed ‘u’ in lowercase in Unix. Here the replacement didn’t take place. |
Highlight "unix">> the small u in unix.
|
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
|
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 where 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
|
Slide:9
Assignment As an assignment, try the below commands and see the output.
|
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: |
|
Slide: Forum for specific Questions |
|
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. |