Difference between revisions of "Linux-Ubuntu/C3/Introduction-to-the-Stream-Editor-sed/English"
Ketkinaina (Talk | contribs) (Created page with "'''Title of script''': '''Introduction to the Stream Editor sed.''' '''Author: EduPyramids.''' '''Keywords: stream editor, sed, display, substitute, insert, delete, context...") |
|||
| Line 1: | Line 1: | ||
| − | '''Title of script''': '''Introduction to the Stream Editor sed | + | '''Title of script''': '''Introduction to the Stream Editor sed''' |
| − | '''Author: EduPyramids | + | '''Author: EduPyramids ''' |
| + | |||
| + | '''Keywords: stream editor, sed, display, substitute, insert, delete, context addressing, Line addressing, edupyramids, video tutorial'''. | ||
| − | |||
{|border=1 | {|border=1 | ||
| Line 38: | Line 39: | ||
[https://EduPyramids.org/ https://EduPyramids.org] | [https://EduPyramids.org/ https://EduPyramids.org] | ||
|| To follow this tutorial, | || To follow this tutorial, | ||
| − | * Learners should have '''Ubuntu '''version''' 24 point 04 | + | * Learners should have '''Ubuntu '''version''' 24 point 04 '''. |
* '''Bash''' version 5 point 1 point 16 and | * '''Bash''' version 5 point 1 point 16 and | ||
* Should know the basics of '''Linux''' terminal. | * Should know the basics of '''Linux''' terminal. | ||
| − | * For the prerequisite '''Linux''' tutorials please visit this website. | + | * For the prerequisite of '''Linux''' tutorials please visit this website. |
|- | |- | ||
|| '''Slide 5''' | || '''Slide 5''' | ||
| Line 61: | Line 62: | ||
|| | || | ||
* '''sed''' is one of the most versatile tools available in Linux. | * '''sed''' is one of the most versatile tools available in Linux. | ||
| − | * It is a '''stream | + | * It is a '''stream editor''' used to process text line by line. |
* '''sed '''can function both as a display tool and an editing tool. | * '''sed '''can function both as a display tool and an editing tool. | ||
* It supports editing operations such as insertion, substitution and deletion. | * It supports editing operations such as insertion, substitution and deletion. | ||
| − | * By default, '''sed '''sends its output to the standard output. | + | * By default, '''sed ''' sends its output to the standard output. |
* The original file remains unchanged unless '''sed''' is explicitly instructed to modify it. | * The original file remains unchanged unless '''sed''' is explicitly instructed to modify it. | ||
|- | |- | ||
| Line 76: | Line 77: | ||
I have a file called '''sed demo dot t x t''' in the home directory. | I have a file called '''sed demo dot t x t''' in the home directory. | ||
| − | This file is provided in the code files folder. Please download it and move it to your Home directory. | + | This file is provided in the code files folder. |
| + | |||
| + | Please download it and move it to your Home directory. | ||
Let us view its contents. | Let us view its contents. | ||
| Line 101: | Line 104: | ||
Hover your mouse on the second line of the output | Hover your mouse on the second line of the output | ||
| − | || Here, '''2''' specifies the location, which is the second line.The letter '''p''' specifies the action, which is printing. | + | || Here, '''2''' specifies the location, which is the second line. |
| + | |||
| + | The letter '''p''' specifies the action, which is printing. | ||
Press '''Enter'''. | Press '''Enter'''. | ||
| Line 118: | Line 123: | ||
'''sed -n '2p' seddemo.txt '''Press''' Enter''' | '''sed -n '2p' seddemo.txt '''Press''' Enter''' | ||
| − | || To print only the second line, type this command and press''' Enter | + | || To print only the second line, type this command and press''' Enter'''. |
|- | |- | ||
|| '''Highlight -n''' | || '''Highlight -n''' | ||
| Line 128: | Line 133: | ||
'''Highlight sed demo.txt''' | '''Highlight sed demo.txt''' | ||
|| Observe that only the second line is printed. | || Observe that only the second line is printed. | ||
| − | * The '''hyphen n '''option suppresses the default output of sed. | + | * The '''hyphen n ''' option suppresses the default output of sed. |
* '''2''' specifies the line number to display or edit. | * '''2''' specifies the line number to display or edit. | ||
* '''p''' specifies the action to perform. | * '''p''' specifies the action to perform. | ||
| Line 136: | Line 141: | ||
This is the general syntax of the '''sed''' command. | This is the general syntax of the '''sed''' command. | ||
|- | |- | ||
| − | || At the prompt type:'''sed -n '$p' seddemo.txt''' Press''' Enter | + | || At the prompt type:'''sed -n '$p' seddemo.txt''' Press''' Enter'''. |
'''Highlight -n''' | '''Highlight -n''' | ||
'''Highlight $ sign''' | '''Highlight $ sign''' | ||
| − | || Now let us print the last line.Type this command and''' '''press '''Enter'''. We see that the last line is displayed. | + | || Now let us print the last line. |
| + | |||
| + | Type this command and''' '''press '''Enter'''. | ||
| + | |||
| + | We see that the last line is displayed. | ||
The '''hyphen n''' suppresses automatic printing of all lines. | The '''hyphen n''' suppresses automatic printing of all lines. | ||
| Line 151: | Line 160: | ||
'''sed -n '3,6p' seddemo.txt '''press''' Enter''' | '''sed -n '3,6p' seddemo.txt '''press''' Enter''' | ||
| − | '''Highlight the output''' | + | '''Highlight the output'''. |
| + | |||
|| Let us just print the entries from the third line to the sixth line of the file. | || Let us just print the entries from the third line to the sixth line of the file. | ||
| − | Type this command and | + | Type this command and press '''Enter'''. |
The output displays the lines from the third line to the sixth line. | The output displays the lines from the third line to the sixth line. | ||
| Line 165: | Line 175: | ||
|| If we want to print all lines except the third to the sixth lines. | || If we want to print all lines except the third to the sixth lines. | ||
| − | Type this command.Any action can be reversed by placing an exclamation mark before the command. | + | Type this command. |
| + | |||
| + | Any action can be reversed by placing an exclamation mark before the command. | ||
This allows us to select the complementary set of lines. | This allows us to select the complementary set of lines. | ||
| Line 226: | Line 238: | ||
It matches lines containing the word computers with small c and capital C. | It matches lines containing the word computers with small c and capital C. | ||
| − | '''p | + | '''p''', prints the matched lines. |
Press '''Enter'''. | Press '''Enter'''. | ||
| Line 238: | Line 250: | ||
This allows us to match any one character present inside the brackets. | This allows us to match any one character present inside the brackets. | ||
| − | When using a pattern in '''sed''', it must be written '''between forward slashes | + | When using a pattern in '''sed''', it must be written '''between forward slashes'''. |
|- | |- | ||
|| Type the command: | || Type the command: | ||
| Line 257: | Line 269: | ||
This is useful for extracting specific lines into a separate file. | This is useful for extracting specific lines into a separate file. | ||
| − | All matching lines are saved in the file '''computer hyphen student | + | All matching lines are saved in the file '''computer hyphen student'''. |
|- | |- | ||
|| Type | || Type | ||
| Line 264: | Line 276: | ||
|| Let us view the content of the '''computer hyphen student '''file. | || Let us view the content of the '''computer hyphen student '''file. | ||
| − | type '''cat''' '''space''' '''computer hyphen student''' and press''' Enter | + | type '''cat''' '''space''' '''computer hyphen student''' and press''' Enter'''. |
We see that lines that match the given pattern are in the file. | We see that lines that match the given pattern are in the file. | ||
| Line 277: | Line 289: | ||
Show the two files created | Show the two files created | ||
| − | || Let us see how we can use '''multiple sed expressions''' in a single command. This allows us to write different matched lines to separate files. | + | || Let us see how we can use '''multiple sed expressions''' in a single command. |
| + | |||
| + | This allows us to write different matched lines to separate files. | ||
Type this command and press '''Enter.''' | Type this command and press '''Enter.''' | ||
| Line 304: | Line 318: | ||
Let us see the contents of the file '''civil ''' | Let us see the contents of the file '''civil ''' | ||
| − | Type '''cat space | + | Type '''cat space civil ''' and press''' Enter.''' |
This displays the entries having the word '''civil'''. | This displays the entries having the word '''civil'''. | ||
| Line 331: | Line 345: | ||
As an assignment, | As an assignment, | ||
| − | Using the text file '''seddemo.txt | + | Using the text file '''seddemo.txt''', |
# Print only the first five lines of the file. | # Print only the first five lines of the file. | ||
# Print all lines that do not contain the word "failed". | # Print all lines that do not contain the word "failed". | ||
| − | # Write all lines containing the word student into a file named '''student-list.txt | + | # Write all lines containing the word student into a file named '''student-list.txt'''. |
|| As an assignment, please do the following. | || As an assignment, please do the following. | ||
|- | |- | ||
Latest revision as of 13:26, 26 January 2026
Title of script: Introduction to the Stream Editor sed
Author: EduPyramids
Keywords: stream editor, sed, display, substitute, insert, delete, context addressing, Line addressing, edupyramids, video tutorial.
| Visual Cue | Narration |
| Slide 1
Title Slide |
Welcome to this spoken tutorial on Introduction to the Stream Editor sed. |
| Slide 2
Learning Objectives |
In this tutorial, we will learn to:
|
| Slide 3
System Requirements |
To record this tutorial, I am using
|
| Slide 4
Pre-requisites |
To follow this tutorial,
|
| Slide 5
Code Files The following code files are required to practice this tutorial.
These files are provided in the Code Files link of this tutorial page. |
|
| Slide 6
Introduction to sed |
|
| Open the file seddemo.txt
Highlight the lines, marks and student details. |
Let us start with a few examples.
We will see how to print lines using the sed command. I have a file called sed demo dot t x t in the home directory. This file is provided in the code files folder. Please download it and move it to your Home directory. Let us view its contents. This file contains entries such as roll no, name, stream, marks, result and the stipend amount. |
| Press Ctrl + Alt + T keys. | Let us open the terminal. |
| Type:sed '2p' seddemo.txt | Let’s print the second line of the file.
Type, sed space single quote 2 p single quote space sed demo dot t x t. |
| Highlight 2
Highlight p Press Enter highlight the output. Hover your mouse on the second line of the output |
Here, 2 specifies the location, which is the second line.
The letter p specifies the action, which is printing. Press Enter. Observe the output. It shows the entire file but the second line appears twice. This is the default behavior of sed. sed prints every line by default The p option prints the specified line again. |
| At the prompt type:
sed -n '2p' seddemo.txt Press Enter |
To print only the second line, type this command and press Enter. |
| Highlight -n
Highlight 2 Highlight p Highlight sed demo.txt |
Observe that only the second line is printed.
This is the general syntax of the sed command. |
| At the prompt type:sed -n '$p' seddemo.txt Press Enter.
Highlight -n Highlight $ sign |
Now let us print the last line.
Type this command and press Enter. We see that the last line is displayed. The hyphen n suppresses automatic printing of all lines. Dollar refers to the last line of the file. |
| At the prompt
sed -n '3,6p' seddemo.txt press Enter Highlight the output. |
Let us just print the entries from the third line to the sixth line of the file.
Type this command and press Enter. The output displays the lines from the third line to the sixth line. |
| Typesed -n '3,6!p' seddemo.txt
Press Enter Show the file on one side and the output comparison |
If we want to print all lines except the third to the sixth lines.
Type this command. Any action can be reversed by placing an exclamation mark before the command. This allows us to select the complementary set of lines. Press Enter. The output displays all lines except lines 3 to 6. |
| So far, we have specified the lines in a file on which an action applies.
This method is known as line addressing. | |
| Slide 7
Line addressing |
Line addressing identifies lines using their position in the file.
It is one way to select specific lines for an action. Another method is called context addressing. |
| Slide 8
Context addressing |
|
| Let us see an example. | |
| Highlight the lines with word computers in the text editor- show the text file seddemo.txt.
At the promptsed -n '/[cC]omputers/p' seddemo.txt Highlight -n highlight /[cC]omputers/ highlight p Press Enter. highlight /[cC]omputers/ |
Suppose we want to print the lines containing the word computers.
Go to the Terminal. Type the following command. hyphen n suppresses default output. This is the Context address. It matches lines containing the word computers with small c and capital C. p, prints the matched lines. Press Enter. Observe the output. The lines containing the word computers are displayed. We enclose parts of the pattern within square brackets. This allows us to match any one character present inside the brackets. When using a pattern in sed, it must be written between forward slashes. |
| Type the command:
sed -n '/[cC]omputers/w computer-student' seddemo.txt press Enter. Open file and show the computer-student |
Let us now see how to write the matched output to a file using the w option.
Type the following command. and Press Enter. Output is redirected to a file instead of getting displayed on the terminal. This is useful for extracting specific lines into a separate file. All matching lines are saved in the file computer hyphen student. |
| Type
cat computer-student andPress Enter |
Let us view the content of the computer hyphen student file.
type cat space computer hyphen student and press Enter. We see that lines that match the given pattern are in the file. |
| Type the following command.sed -n -e '/electronics/w electro' -e '/civil/w civil' seddemo.txtPress Enter
Highlight -e Highlight /electronics/w electro Highlight /civil/w civil Show the two files created |
Let us see how we can use multiple sed expressions in a single command.
This allows us to write different matched lines to separate files. Type this command and press Enter. The hyphen e option allows combining multiple sed commands in a single line. slash electronics slash w electro writes lines matching electronics to electro file. slash civil slash w civil writes lines matching civil to civil file. After running the command, two text files, electro and civil, are created with the matched lines. |
| cat electro Press Enter
Highlight the contents cat civil Press Enter Highlight the contents |
To see what they contain, type cat space electro and press Enter.
This displays the entries with the word electronics. Let us see the contents of the file civil Type cat space civil and press Enter. This displays the entries having the word civil. |
| We will see more sed commands in the upcoming tutorial. | |
| Slide 9
Summary In this tutorial, we have learnt to:
|
With this we come to the end of this tutorial.
Let us summarise. |
| Slide 10
Assignment As an assignment, Using the text file seddemo.txt,
|
As an assignment, please do the following. |
| Slide 11
Thank you |
This Spoken Tutorial is brought to you by EduPyramids Educational Services Private Limited SINE IIT Bombay.
Thank you. |