Difference between revisions of "Sed-Stream-Editor/C2/Sed-commands-with-Files/English"
(Created page with " {| border="1" |- || '''VISUAL CUE''' || '''NARRATION''' |- || '''Slide 1:''' || Welcome to the spoken tutorial''' '''on '''Sed Commands with Files.''' |- || '''Slide 2:'''...") |
Nancyvarkey (Talk | contribs) |
||
Line 7: | Line 7: | ||
|- | |- | ||
|| '''Slide 1:''' | || '''Slide 1:''' | ||
− | || Welcome to the spoken tutorial | + | || Welcome to the spoken tutorial on '''Sed Commands with Files.''' |
|- | |- | ||
|| '''Slide 2:''' | || '''Slide 2:''' | ||
Line 13: | Line 13: | ||
'''Learning Objectives''' | '''Learning Objectives''' | ||
|| In this tutorial, we will learn about | || In this tutorial, we will learn about | ||
− | * Delete command | + | * '''Delete command''' |
− | * Write command | + | * '''Write command''' |
− | * Read command | + | * '''Read command''' |
− | * Append command | + | * '''Append command''' |
− | * Insert command | + | * '''Insert command''' |
− | * Change command | + | * '''Change command''' |
− | * Execute command | + | * '''Execute 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 relevant spoken tutorials on this website. | If not, then go through the relevant spoken tutorials on this website. | ||
Line 38: | Line 37: | ||
'''Delete Command''' | '''Delete Command''' | ||
− | || First we will see about the delete command in sed | + | || First we will see about the '''delete command''' in '''sed''' |
− | * '''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. | + | * This '''command''' removes lines only from the '''pattern buffer'''. |
− | * The original file remains unchanged. | + | * The original '''file''' remains unchanged. |
|- | |- | ||
|| '''Slide 5(b):''' | || '''Slide 5(b):''' | ||
Line 49: | Line 48: | ||
'''Highlight according to narration''' | '''Highlight according to narration''' | ||
− | || This is the syntax for the delete command.* '''range1, range2''' are the starting and ending range. | + | || This is the '''syntax''' for the '''delete command'''. |
− | * It can be either a line number or pattern strings. | + | * '''range1, range2''' are the starting and ending range. |
− | * '''d '''specifies the delete option | + | * It can be either a '''line''' number or '''pattern strings'''. |
+ | * '''d '''specifies the '''delete''' option | ||
|- | |- | ||
|| '''Slide 6:''' | || '''Slide 6:''' | ||
Line 62: | Line 62: | ||
|- | |- | ||
|| 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 | + | Press the '''Enter''' key after every '''command'''. |
|- | |- | ||
|| >> cat '''IndianBooks.txt''' | || >> cat '''IndianBooks.txt''' | ||
− | || Let us open the file '''IndianBooks.txt '''which''' ''' | + | || 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. | + | I’ll be using this file for demonstration of various file '''commands'''. |
|- | |- | ||
|| | || | ||
> '''sed ‘d’ IndianBooks.txt''' | > '''sed ‘d’ IndianBooks.txt''' | ||
− | ||First we will see how the '''delete''' | + | ||First we will see how the '''delete command''' is used in files. |
− | Type the command as shown | + | Type the '''command''' as shown. |
− | We couldn’t see any output. Why? | + | We couldn’t see any '''output'''. Why? |
− | We didn’t give any line address. So '''Sed''' operates on every line by default. | + | We didn’t give any '''line address'''. So '''Sed''' operates on every '''line''' by default. |
− | It deletes all the lines from the pattern buffer. | + | It '''deletes''' all the '''lines''' from the '''pattern buffer'''. |
− | Hence it does not print anything on the standard output. | + | Hence it does not print anything on the '''standard output'''. |
|- | |- | ||
|| >'''sed ‘4d’ IndianBooks.txt''' | || >'''sed ‘4d’ IndianBooks.txt''' | ||
Point to the output | Point to the output | ||
− | || Let us give the specific line number as shown for the '''Sed ''' | + | || Let us give the specific '''line''' number as shown for the '''Sed command''' to work. |
− | We can see the 4th line is deleted. | + | We can see the 4th '''line''' is '''deleted'''. |
|- | |- | ||
|| '''sed ‘2,6d’ IndianBooks.txt''' | || '''sed ‘2,6d’ IndianBooks.txt''' | ||
− | || Type the command as shown. | + | || Type the '''command''' as shown. |
− | We can also give a range of line numbers that can be deleted. | + | We can also give a range of '''line''' numbers that can be '''deleted'''. |
− | This command deletes all the lines from 2 to 6. | + | This '''command deletes''' all the '''lines''' from 2 to 6. |
|- | |- | ||
||'''sed ‘/R K Narayan/d’ IndianBooks.txt''' | ||'''sed ‘/R K Narayan/d’ IndianBooks.txt''' | ||
− | ||We can specify patterns instead of line numbers as shown here. | + | ||We can specify '''patterns''' instead of '''line''' numbers as shown here. |
− | This has | + | This has '''delete'''d the '''lines''' which have the author name as '''R K Narayan.''' |
|- | |- | ||
|| | || | ||
Line 111: | Line 111: | ||
>sed ‘/Malgudi/,/Crescent/d’ '''IndianBooks'''.txt | >sed ‘/Malgudi/,/Crescent/d’ '''IndianBooks'''.txt | ||
− | ||We can also specify a range using a textual pattern. | + | ||We can also specify a range using a textual '''pattern'''. |
− | For example, we want to remove all the lines between the | + | 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 ''' | + | 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. | + | Look at the '''output'''. |
|- | |- | ||
|| >sed ‘$d’ '''IndianBooks.txt''' | || >sed ‘$d’ '''IndianBooks.txt''' | ||
'''Highlight $d''' | '''Highlight $d''' | ||
− | || Type the command as shown. | + | || Type the '''command''' as shown. |
− | We can delete the last line of a file using the special character dollar $. | + | We can '''delete''' the last '''line''' of a file using the '''special character dollar $.''' |
|- | |- | ||
|| >sed ‘1!d’ '''IndianBooks.txt''' | || >sed ‘1!d’ '''IndianBooks.txt''' | ||
Line 130: | Line 130: | ||
'''Highlight 1d''' | '''Highlight 1d''' | ||
− | || Next we will see how to use the negation operator. | + | || Next we will see how to use the '''negation''' operator. |
− | Type the command as shown. | + | Type the '''command''' as shown. |
− | Here ‘1’ specifies the line number of the file. | + | Here ‘1’ specifies the '''line''' number of the file. |
− | The negation | + | The '''negation''' operator with '''d '''indicates, not to '''delete''' the first '''line'''. |
− | We can see | + | We can see this '''sed command''' removes all the '''lines''' except the first '''line'''. |
|- | |- | ||
|| > sed ‘1d;$d’ '''IndianBooks.txt''' | || > sed ‘1d;$d’ '''IndianBooks.txt''' | ||
− | || This command will delete the first and last | + | || This '''command''' will '''delete''' the first and last '''lines'''. |
− | We can specify the list of lines to be removed with semicolon as a delimiter. | + | We can specify the list of '''lines''' to be removed with '''semicolon''' as a '''delimiter'''. |
|- | |- | ||
|| sed ‘/^1/d’ '''IndianBooks.txt''' | || sed ‘/^1/d’ '''IndianBooks.txt''' | ||
− | || This command removes all the lines that begin with ‘1’. | + | || This '''command''' removes all the '''lines''' that begin with ‘1’. |
− | Recall that the caret(^) symbol specifies the starting of a line. | + | Recall that the '''caret(^) symbol''' specifies the starting of a '''line'''. |
− | We can see that line 1 and line 10 are | + | We can see that '''line''' 1 and '''line''' 10 are '''delete'''d. |
|- | |- | ||
|| >sed ‘/3$/d’ <'''IndianBooks.txt''' | || >sed ‘/3$/d’ <'''IndianBooks.txt''' | ||
− | || Type the command as shown. | + | || Type the '''command''' as shown. |
− | This command deletes all the lines that end with '3'. | + | This '''command deletes''' all the '''lines''' that end with '3'. |
− | $ is to indicate the end of the line. | + | '''$ dollar''' is to indicate the end of the '''line'''. |
− | We can see that the lines ending with 3 in the published year are | + | We can see that the '''lines''' ending with 3 in the published year are '''delete'''d. |
|- | |- | ||
|| >sed '/[38]$/d' '''IndianBooks.txt''' | || >sed '/[38]$/d' '''IndianBooks.txt''' | ||
− | || This sed command | + | || This '''sed command delete'''s all the '''lines''' that end with either '3' or '8'. |
− | Note that the lines ending with 3 or 8 in the published year are | + | Note that the '''lines''' ending with '3' or '8' in the published year are '''delete'''d. |
|- | |- | ||
|| '''Slide 7: Delete command: Note:''' | || '''Slide 7: Delete command: Note:''' | ||
− | ||In all the above examples, the sed command does not remove the lines from the source file. | + | ||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. | + | 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 | + | 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. | + | Otherwise, you can redirect the output of the '''sed command''' to another file. |
|- | |- | ||
|| | || | ||
− | ||Next we will see about the write command. | + | ||Next we will see about the '''write command'''. |
|- | |- | ||
||'''Slide 8:''' | ||'''Slide 8:''' | ||
'''Write command''' | '''Write command''' | ||
− | ||Sed reads a line and places it in a pattern buffer. | + | ||'''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. | + | Then writes the '''pattern buffer''' to the given '''output file''' according to the given options. |
|- | |- | ||
|| '''Slide 9:''' | || '''Slide 9:''' | ||
Line 196: | Line 196: | ||
Syntax: sed 'ADDRESS w outputfile' inputfile | Syntax: sed 'ADDRESS w outputfile' inputfile | ||
− | || Here '''‘w'''’ represents the write command. It should be at the end of all options. | + | || Here '''‘w'''’ represents the '''write command'''. It should be at the end of all '''options'''. |
− | '''Address''' can be either line numbers or pattern strings. | + | '''Address''' can be either '''line''' numbers or '''pattern strings'''. |
− | '''outputfile '''is the filename where the output has to be stored. | + | '''outputfile '''is the filename where the '''output''' has to be stored. |
− | '''inputfile''' is the input file for the command to execute. | + | '''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. | + | 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. | + | || Switch back to the '''terminal'''. |
|- | |- | ||
||sed -n ‘w books_copy.txt’ '''IndianBooks.txt''' | ||sed -n ‘w books_copy.txt’ '''IndianBooks.txt''' | ||
− | || Type the write command as shown: | + | || Type the '''write command''' as shown: |
If you open '''books_copy.txt''', you will see the copy of the content of '''IndianBooks.txt''' | If you open '''books_copy.txt''', you will see the copy of the content of '''IndianBooks.txt''' | ||
Line 219: | Line 219: | ||
> sed -n ‘1~2 w out.txt’ '''IndianBooks'''.txt | > sed -n ‘1~2 w out.txt’ '''IndianBooks'''.txt | ||
− | || We can copy only certain line numbers to another file. | + | || We can copy only certain '''line''' numbers to another file. |
− | Type the command to copy all odd lines 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''' | + | We can see only the odd '''lines''' are copied to''' out.txt''' |
|- | |- | ||
|| sed -n ‘/Gandhi/ w GandhiBooks.txt’ '''IndianBooks.txt''' | || sed -n ‘/Gandhi/ w GandhiBooks.txt’ '''IndianBooks.txt''' | ||
− | || Type the command as shown. | + | || Type the '''command''' as shown. |
− | We can give patterns that have to be copied to another file. | + | 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. | + | This '''command''' will store the books with author name '''Gandhi''' to another file. |
− | Let us see the | + | Let us see the '''output file GandhiBooks.txt''' |
|- | |- | ||
|| | || | ||
− | || Next we will see the ''' | + | || Next we will see the '''read command'''. |
|- | |- | ||
|| '''Slide 10:''' | || '''Slide 10:''' | ||
'''Read Command''' | '''Read Command''' | ||
− | || Using the | + | || 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. | + | It appends with each '''line''' of the '''input file''' as specified in the '''sed command pattern'''. |
|- | |- | ||
|| | || | ||
− | || Switch back to the terminal. | + | || Switch back to the '''terminal'''. |
|- | |- | ||
|| > cat '''new_books.txt''' | || > cat '''new_books.txt''' | ||
− | || Let us open the | + | || Let us open the '''file new_books.txt '''which has 3 '''lines'''. |
|- | |- | ||
|| '''sed ‘4 r< new_books.txt’ IndianBooks.txt''' | || '''sed ‘4 r< new_books.txt’ IndianBooks.txt''' | ||
− | || This command instructs sed to read the content from '''new_book.txt .''' | + | || This '''command''' instructs '''sed''' to read the content from '''new_book.txt.''' |
− | Then it inserts after the 4th line in '''IndianBooks.txt''' | + | Then it inserts after the 4th '''line''' in '''IndianBooks.txt''' |
− | Here 4 specifies the line number and r specifies the read command. | + | Here '''4''' specifies the '''line''' number and '''r''' specifies the '''read command'''. |
|- | |- | ||
|| '''Slide 11:''' | || '''Slide 11:''' | ||
'''Assignment''' | '''Assignment''' | ||
− | ||As an assignment, try the below commands and see the output. | + | ||As an assignment, try the below '''commands''' and see the output. |
'''sed ‘4,6 r new_books.txt’ IndianBooks.txt''' | '''sed ‘4,6 r new_books.txt’ IndianBooks.txt''' | ||
|- | |- | ||
|| '''Append command''' | || '''Append command''' | ||
− | || Next we will see about the ''' | + | || Next we will see about the '''append command'''. |
|- | |- | ||
|| | || | ||
'''> sed ‘$ a 11. Broken wings, Sarojini Naidu, 1916’ IndianBooks.txt''' | '''> sed ‘$ a 11. Broken wings, Sarojini Naidu, 1916’ IndianBooks.txt''' | ||
− | || Let us append a new book entry at the end of the '''IndianBooks | + | || 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. | + | Type the '''command''' with the new book entry as shown. |
− | Here | + | Here '''a''' indicates '''append command''' and '''$ dollar''' indicates the end of the '''file'''. |
|- | |- | ||
|| '''>sed ‘6 a 11. Broken wings, Sarojini Naidu, 1916’ IndianBooks.txt''' | || '''>sed ‘6 a 11. Broken wings, Sarojini Naidu, 1916’ IndianBooks.txt''' | ||
− | || Suppose if we want to insert in between the lines. | + | || Suppose if we want to insert in between the '''lines'''. |
− | Type the command with line number as shown. | + | Type the '''command''' with '''line''' number as shown. |
− | Here 6 indicates the line number and | + | Here '6' indicates the '''line''' number and '''a''' is the '''append command'''. |
|- | |- | ||
|| '''>sed ‘/Hind Swaraj/ a 11. Broken wings, Sarojini Naidu, 1916’ IndianBooks.txt''' | || '''>sed ‘/Hind Swaraj/ a 11. Broken wings, Sarojini Naidu, 1916’ IndianBooks.txt''' | ||
Highlight /'''Hind Swaraj/''' | Highlight /'''Hind Swaraj/''' | ||
− | || This command specifies a text pattern instead of line number. | + | || This '''command''' specifies a '''text pattern''' instead of '''line''' number. |
Look at the output. | Look at the output. | ||
− | The new book entry has appended after the pattern ‘Hind swaraj’. | + | The new book entry has appended after the '''pattern ‘Hind swaraj’'''. |
|- | |- | ||
|| '''> sed ‘/My/ a 11. Broken wings, Sarojini Naidu, 1916’ IndianBooks.txt''' | || '''> sed ‘/My/ a 11. Broken wings, Sarojini Naidu, 1916’ IndianBooks.txt''' | ||
Line 298: | Line 298: | ||
Highlight /'''My/''' | Highlight /'''My/''' | ||
− | || Type this command: | + | || Type this '''command''': |
− | If there are multiple patterns matching, then the text is appended after each match. | + | If there are multiple '''patterns matching''', then the text is appended after each match. |
|- | |- | ||
|| | || | ||
− | || Next we will see the ''' | + | || Next we will see the '''insert command'''. |
|- | |- | ||
|| '''Insert command''' | || '''Insert command''' | ||
Line 310: | Line 310: | ||
'''Highlight 3 and ‘i’''' | '''Highlight 3 and ‘i’''' | ||
− | || Insert command is the same as append command. | + | || '''Insert command''' is the same as '''append command'''. |
− | But it inserts a line before a specific position. | + | But it inserts a '''line''' before a specific position. |
− | Type the command as shown: | + | Type the '''command''' as shown: |
− | This command inserts the new text before the third line. | + | This '''command''' inserts the new text before the third '''line'''. |
− | Here 3 is the line number and ‘i’ represents the insert command. | + | Here 3 is the '''line''' number and ‘i’ represents the '''insert command'''. |
|- | |- | ||
|| '''change command''' | || '''change command''' | ||
− | || Next we will see about the '''change''' or | + | || Next we will see about the '''change''' or '''replace command'''. |
− | This command replaces the existing line with new text. | + | This '''command''' replaces the existing '''line''' with new text. |
|- | |- | ||
|| '''> sed ‘3 c 3. Our films, Their Films, Satyajit Ray, 1976’ IndianBooks.txt''' | || '''> 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. | + | || Let us replace the third '''line''' in''' IndianBooks.txt '''with some new text. |
− | Type the command as shown. | + | Type the '''command''' as shown. |
− | ‘c’ indicates the change command. | + | '''‘c’''' indicates the '''change command'''. |
− | We can see the third line is replaced with the new text. | + | 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''' | || '''> sed ‘6, 8 c 3. Our films, Their Films, Satyajit Ray, 1976’ IndianBooks.txt''' | ||
− | || We can also replace multiple lines with a single line. | + | || We can also replace multiple '''lines''' with a single '''line'''. |
− | Type the command as shown. | + | Type the '''command''' as shown. |
− | The lines 6 to 8 are replaced with the new text. | + | The '''lines''' '6' to '8' are replaced with the new text. |
|- | |- | ||
|| | || | ||
− | || Next we will see the execute command. | + | || Next we will see the '''execute command'''. |
|- | |- | ||
|| '''Execute command''' | || '''Execute command''' | ||
'''>> sed ‘4 e date’ IndianBooks.txt''' | '''>> sed ‘4 e date’ IndianBooks.txt''' | ||
− | || Type the command as shown. | + | || Type the '''command''' as shown. |
− | '''e''' represent the execute command. | + | '''e''' represent the '''execute command.''' |
− | This will execute the external command and insert at the matched pattern. | + | This will '''execute''' the external '''command''' and insert at the matched '''pattern'''. |
− | We can see the date is inserted at the fourth line. | + | We can see the date is inserted at the fourth '''line'''. |
|- | |- | ||
|| '''Slide 12:''' | || '''Slide 12:''' | ||
Line 362: | Line 362: | ||
|| | || | ||
|| In this tutorial, we learnt | || In this tutorial, we learnt | ||
− | * Delete command | + | * '''Delete command''' |
− | * Write command | + | * '''Write command''' |
− | * Read command | + | * '''Read command''' |
− | * Append command | + | * '''Append command''' |
− | * Insert command | + | * '''Insert command''' |
− | * Change command | + | * '''Change command''' |
− | * Execute command | + | * '''Execute command''' |
|- | |- | ||
Line 374: | Line 374: | ||
'''Assignment:''' | '''Assignment:''' | ||
− | || As an | + | || As an assignment, try the below '''commands''' and see the output. |
− | > sed ‘4,6 r date’ | + | > '''sed ‘4,6 r date’ IndianBooks.txt''' |
− | > sed ‘/My/ e date’ | + | > '''sed ‘/My/ e date’ IndianBooks.txt''' |
|- | |- | ||
|| '''Slide 14:''' | || '''Slide 14:''' |
Latest revision as of 14:36, 15 March 2021
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
|
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
|
Slide 5(b):
Syntax: sed [range1][range2]d filename Highlight according to narration |
This is the syntax for the delete command.
|
Slide 6:
Code files - IndianBooks.txt |
|
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
| |
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:
|
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. |