Difference between revisions of "BOSS-Linux/C3/The-sed-command/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 5: | Line 5: | ||
|- | |- | ||
| 00:01 | | 00:01 | ||
− | | Welcome to this spoken tutorial on '''sed''' - the stream editor. | + | | Welcome to this spoken tutorial on '''sed''' - the '''stream editor'''. |
|- | |- | ||
| 00:07 | | 00:07 | ||
− | | In this tutorial we will learn usage of '''sed '''command. | + | | In this tutorial, we will learn usage of '''sed '''command. |
|- | |- | ||
| 00:11 | | 00:11 | ||
− | |We will do through some examples | + | |We will do through some examples. |
|- | |- | ||
Line 29: | Line 29: | ||
|- | |- | ||
| 00:33 | | 00:33 | ||
− | | As prerequisites | + | | As prerequisites, |
|- | |- | ||
| 00:35 | | 00:35 | ||
− | | | + | |you should know basics of '''Linux terminal'''. |
|- | |- | ||
| 00:38 | | 00:38 | ||
− | |For relevant tutorials please visit our website which as shown: http://spoken-tutorial.org | + | |For relevant tutorials, please visit our website which is as shown: http://spoken-tutorial.org |
|- | |- | ||
| 00:44 | | 00:44 | ||
− | | Let us with an introduction to '''sed''' | + | | Let us start with an introduction to '''sed'''. |
|- | |- | ||
|00:47 | |00:47 | ||
− | |'''sed''' is a stream editor. | + | |'''sed''' is a '''stream editor'''. |
|- | |- | ||
Line 53: | Line 53: | ||
|- | |- | ||
| 00:57 | | 00:57 | ||
− | |It performs some display or editing function | + | |It performs some display or editing function- |
|- | |- | ||
Line 69: | Line 69: | ||
|- | |- | ||
| 01:18 | | 01:18 | ||
− | |I have a file called '''seddemo.txt''' in home directory version. | + | |I have a file called '''seddemo.txt''' in '''home''' directory version. |
|- | |- | ||
| 01:23 | | 01:23 | ||
− | |Let us view its | + | |Let us view its contents. |
|- | |- | ||
| 01:26 | | 01:26 | ||
− | |In this file we have some | + | |In this file, we have some entries like- roll no, name, stream, marks, pass or fail and the stipend amount. |
|- | |- | ||
| 01:38 | | 01:38 | ||
− | |Now suppose we want to print the second line of the file. | + | |Now, suppose we want to print the second line of the file. |
|- | |- | ||
| 01:43 | | 01:43 | ||
− | |For this we need to open the terminal | + | |For this, we need to open the terminal. |
|- | |- | ||
| 01:47 | | 01:47 | ||
− | |Now | + | |Now, type: |
|- | |- | ||
Line 97: | Line 97: | ||
|- | |- | ||
| 01:57 | | 01:57 | ||
− | |Press '''Enter''' | + | |Press '''Enter'''. |
|- | |- | ||
Line 105: | Line 105: | ||
|- | |- | ||
| 02:05 | | 02:05 | ||
− | | | + | |'p' denotes the action which is printing ('''p'''). |
|- | |- | ||
| 02:09 | | 02:09 | ||
− | |Now | + | |Now look at the output. |
|- | |- | ||
Line 117: | Line 117: | ||
|- | |- | ||
| 02:18 | | 02:18 | ||
− | |This is the default | + | |This is the default behavior of the action '''p'''. |
|- | |- | ||
| 02:22 | | 02:22 | ||
− | |To only print the second line | + | |To only print the second line, |
|- | |- | ||
| 02:25 | | 02:25 | ||
− | | | + | |type: '''sed '''space '''-n''' space (Within single quotes) '''2p''' after the single quotes space '''seddemo.txt ''' |
|- | |- | ||
Line 133: | Line 133: | ||
|- | |- | ||
|02:40 | |02:40 | ||
− | |We see only the second line | + | |We see only the second line is printed. |
|- | |- | ||
Line 141: | Line 141: | ||
|- | |- | ||
| 02:51 | | 02:51 | ||
− | |Then we give the location in the stream that we want to edit or display. | + | |Then we give the location in the '''stream''' that we want to edit or display. |
|- | |- | ||
Line 149: | Line 149: | ||
|- | |- | ||
| 03:00 | | 03:00 | ||
− | |'''p''' indicates the action we want to take | + | |'''p''' indicates the action we want to take i.e. to print the second line. |
|- | |- | ||
| 03:06 | | 03:06 | ||
− | | | + | |and '''seddemo.txt''' is the name of the file. |
|- | |- | ||
| 03:11 | | 03:11 | ||
− | |This is the general syntax of sed command. | + | |This is the general syntax of ''''sed'''' '''command'''. |
|- | |- | ||
| 03:15 | | 03:15 | ||
− | |Now let us print the last line of the file. | + | |Now, let us print the last line of the file. |
|- | |- | ||
|03:20 | |03:20 | ||
− | |Let me clear the | + | |Let me clear the prompt. |
|- | |- | ||
| 03:24 | | 03:24 | ||
− | |Now | + | |Now, type: |
|- | |- | ||
Line 185: | Line 185: | ||
|- | |- | ||
| 03:42 | | 03:42 | ||
− | |Now come back to text editor. | + | |Now come back to '''text editor'''. |
|- | |- | ||
| 03:45 | | 03:45 | ||
− | | Suppose we want to print the | + | | Suppose, we want to print the entries from 3rd to 6th. |
|- | |- | ||
| 03:50 | | 03:50 | ||
− | |For this we need to type on the terminal: | + | |For this, we need to type on the terminal: |
|- | |- | ||
Line 201: | Line 201: | ||
|- | |- | ||
| 04:07 | | 04:07 | ||
− | |''' | + | |Press '''Enter.''' |
|- | |- | ||
Line 209: | Line 209: | ||
|- | |- | ||
| 04:14 | | 04:14 | ||
− | | Any of the actions can be reversed by using the exclamation mark before the action. | + | | Any of the actions can be reversed by using the exclamation mark (!) before the action. |
|- | |- | ||
| 04:21 | | 04:21 | ||
− | |Say if we had to print all lines except from 3rd to 6th we will type: | + | |Say, if we had to print all lines except from 3rd to 6th we will type: |
'''sed''' space '''-n''' space within single quotes ‘'''3''' (comma) ''',6''' (exclamation mark) '''!p''' | '''sed''' space '''-n''' space within single quotes ‘'''3''' (comma) ''',6''' (exclamation mark) '''!p''' | ||
Line 222: | Line 222: | ||
|- | |- | ||
| 04:45 | | 04:45 | ||
− | | | + | |Press '''Enter.''' |
|- | |- | ||
Line 242: | Line 242: | ||
|- | |- | ||
| 05:02 | | 05:02 | ||
− | |This is known as line addressing. | + | |This is known as '''line addressing'''. |
|- | |- | ||
Line 262: | Line 262: | ||
|- | |- | ||
| 05:21 | | 05:21 | ||
− | |If we want to take actions on lines that contain a particular word we use context addressing. | + | |If we want to take actions on lines that contain a particular word, we use context addressing. |
|- | |- | ||
| 05:29 | | 05:29 | ||
− | |Regular expressions can be used. | + | |'''Regular expressions''' can be used. |
|- | |- | ||
Line 278: | Line 278: | ||
|- | |- | ||
| 05:38 | | 05:38 | ||
− | | Say we want to print those lines which have the word | + | | Say, we want to print those lines which have the word "computers". |
|- | |- | ||
Line 286: | Line 286: | ||
|- | |- | ||
| 05:46 | | 05:46 | ||
− | |Now | + | |Now, type: |
|- | |- | ||
Line 294: | Line 294: | ||
|- | |- | ||
| 06:14 | | 06:14 | ||
− | | | + | |Press '''Enter.''' |
|- | |- | ||
| 06:16 | | 06:16 | ||
− | |We see the lines with the word computers is displayed. | + | |We see the lines with the word "computers" is displayed. |
|- | |- | ||
| 06:22 | | 06:22 | ||
− | |We write pattern within square brackets. | + | |We write pattern within square-brackets. |
|- | |- | ||
| 06:24 | | 06:24 | ||
− | |This is to match any one or both of the characters within square brackets. | + | |This is to match any one or both of the characters within square-brackets. |
|- | |- | ||
| 06:30 | | 06:30 | ||
− | |When we need to match patterns the pattern needs to be typed between front slashes. | + | |When we need to match patterns, the pattern needs to be typed between front slashes. |
|- | |- | ||
| 06:37 | | 06:37 | ||
− | | We can print it in file as well using the '''w''' option. | + | | We can print it in a file as well using the '''w''' option. |
|- | |- | ||
| 06:43 | | 06:43 | ||
− | |For this type: | + | |For this, type: |
|- | |- | ||
Line 326: | Line 326: | ||
|- | |- | ||
| 07:11 | | 07:11 | ||
− | | | + | |Press '''Enter.''' |
|- | |- | ||
Line 334: | Line 334: | ||
|- | |- | ||
| 07:21 | | 07:21 | ||
− | | Let us view the | + | | Let us view the contents. |
|- | |- | ||
| 07:25 | | 07:25 | ||
− | | Type '''cat''' space '''computer_student'''.txt | + | | Type: '''cat''' space '''computer_student'''.txt |
|- | |- | ||
| 07:32 | | 07:32 | ||
− | |''' | + | |Press '''Enter.''' |
|- | |- | ||
| 07:35 | | 07:35 | ||
− | |We see the entries | + | |We see the entries. |
|- | |- | ||
Line 354: | Line 354: | ||
|- | |- | ||
| 07:42 | | 07:42 | ||
− | | Let us clear the | + | | Let us clear the prompt. |
|- | |- | ||
| 07:45 | | 07:45 | ||
− | | Type '''sed''' space '''-n''' space '''-e''' space (within single quotes) (front slash) '''‘/electronics/w''' space '''electro.txt’''' after the single quotes space '''-e''' space (within single quotes) (front slash) '''‘/civil/w''' space '''civil.txt’''' after the single quote space '''seddemo.txt ''' | + | | Type: '''sed''' space '''-n''' space '''-e''' space (within single quotes) (front slash) '''‘/electronics/w''' space '''electro.txt’''' after the single quotes space '''-e''' space (within single quotes) (front slash) '''‘/civil/w''' space '''civil.txt’''' after the single quote space '''seddemo.txt ''' |
|- | |- | ||
| 08:18 | | 08:18 | ||
− | |Press '''Enter''' | + | |Press '''Enter'''. |
|- | |- | ||
| 08:22 | | 08:22 | ||
− | |Here '''-e''' is used to combine multiple methods | + | |Here '''-e''' is used to combine multiple methods. |
|- | |- | ||
|08:27 | |08:27 | ||
− | |This would create two files '''electro.txt''' and '''civil.txt'''. | + | |This would create two files- '''electro.txt''' and '''civil.txt'''. |
|- | |- | ||
|08:34 | |08:34 | ||
− | | To see what they contain type: | + | | To see what they contain, type: |
|- | |- | ||
Line 382: | Line 382: | ||
|- | |- | ||
| 08:42 | | 08:42 | ||
− | |This will display the | + | |This will display the entries with the word "electronics". |
|- | |- | ||
| 08:47 | | 08:47 | ||
− | |Let us see the content of civil file. | + | |Let us see the content of "civil" file. |
|- | |- | ||
| 08:50 | | 08:50 | ||
− | | Type '''cat '''space''' civil.txt''' | + | | Type: '''cat '''space''' civil.txt''' |
|- | |- | ||
| 08:55 | | 08:55 | ||
− | |''' | + | | press '''Enter'''. |
|- | |- | ||
| 08:57 | | 08:57 | ||
− | |This will display the | + | |This will display the entries having the word "civil". |
|- | |- | ||
Line 414: | Line 414: | ||
|- | |- | ||
|09:11 | |09:11 | ||
− | |Let us switch back to our slide | + | |Let us switch back to our slide. |
|- | |- | ||
| 09:14 | | 09:14 | ||
− | | we will summarize | + | | we will summarize. |
|- | |- | ||
| 09:15 | | 09:15 | ||
− | |In this tutorial we learnt | + | |In this tutorial, we learnt: '''sed''' |
|- | |- | ||
| 09:18 | | 09:18 | ||
− | |To print using '''sed.''' | + | |* To print using '''sed.''' |
|- | |- | ||
| 09:19 | | 09:19 | ||
− | |Line Addressing. | + | |* Line Addressing. |
|- | |- | ||
| 09:21 | | 09:21 | ||
− | |Context Addressing. | + | |* Context Addressing. |
|- | |- | ||
| 09:23 | | 09:23 | ||
− | | | + | | As an assignment, |
|- | |- | ||
| 09:25 | | 09:25 | ||
− | |Use the same text file | + | |Use the same text file "seddemo.txt". |
|- | |- | ||
Line 450: | Line 450: | ||
|- | |- | ||
| 09:33 | | 09:33 | ||
− | | Watch the video available at the link shown below | + | | Watch the video available at the link shown below. |
|- | |- | ||
| 09:36 | | 09:36 | ||
− | |It | + | |It summarizes the Spoken Tutorial project. |
|- | |- | ||
| 09:39 | | 09:39 | ||
− | |If you do not have good bandwidth, you can download and watch it | + | |If you do not have good bandwidth, you can download and watch it. |
|- | |- | ||
| 09:44 | | 09:44 | ||
− | | The Spoken Tutorial | + | | The Spoken Tutorial project team: |
|- | |- | ||
| 09:46 | | 09:46 | ||
− | |Conducts workshops using spoken tutorials | + | |Conducts workshops using spoken tutorials. |
|- | |- | ||
| 09:49 | | 09:49 | ||
− | |Gives certificates to those who pass an online test | + | |Gives certificates to those who pass an online test. |
|- | |- | ||
Line 478: | Line 478: | ||
|- | |- | ||
| 10:00 | | 10:00 | ||
− | | Spoken Tutorial | + | | '''Spoken Tutorial''' project is a part of the '''Talk to a Teacher''' project. |
|- | |- | ||
| 10:04 | | 10:04 | ||
− | |It is supported by the National Mission on Education through ICT, MHRD, Government of India | + | |It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
|- | |- | ||
| 10:11 | | 10:11 | ||
− | |More information on this | + | |More information on this mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
|- | |- | ||
| 10:17 | | 10:17 | ||
− | |This is Ashwini Patil from IIT Bombay signing off . Thank You for joining. | + | |This is Ashwini Patil from IIT Bombay, signing off. Thank You for joining. |
|} | |} |
Revision as of 12:17, 13 August 2015
Time | Narration |
00:01 | Welcome to this spoken tutorial on sed - the stream editor. |
00:07 | In this tutorial, we will learn usage of sed command. |
00:11 | We will do through some examples. |
00:14 | To record this tutorial, |
00:16 | I am using Linux Operating System and GNU BASH version 4.2.24 |
00:26 | Please note, GNU bash version 4 or above is recommended to practice this tutorial. |
00:33 | As prerequisites, |
00:35 | you should know basics of Linux terminal. |
00:38 | For relevant tutorials, please visit our website which is as shown: http://spoken-tutorial.org |
00:44 | Let us start with an introduction to sed. |
00:47 | sed is a stream editor. |
00:50 | sed finds some pattern of text in a particular location of a file. |
00:57 | It performs some display or editing function- |
01:01 | editing function like insertion, substitution and deletion in matched text. |
01:10 | Let us first start with some examples. |
01:13 | We will see how to print using the sed command. |
01:18 | I have a file called seddemo.txt in home directory version. |
01:23 | Let us view its contents. |
01:26 | In this file, we have some entries like- roll no, name, stream, marks, pass or fail and the stipend amount. |
01:38 | Now, suppose we want to print the second line of the file. |
01:43 | For this, we need to open the terminal. |
01:47 | Now, type: |
01:48 | sed space within single quotes ‘2p’ after the single quotes space seddemo.txt |
01:57 | Press Enter. |
02:00 | Here 2 denotes the location which is the second line. |
02:05 | 'p' denotes the action which is printing (p). |
02:09 | Now look at the output. |
02:11 | It shows the entire file but see that the second line is printed twice. |
02:18 | This is the default behavior of the action p. |
02:22 | To only print the second line, |
02:25 | type: sed space -n space (Within single quotes) 2p after the single quotes space seddemo.txt |
02:37 | Press Enter. |
02:40 | We see only the second line is printed. |
02:44 | -n stands for ‘silent mode’ which will suppress all unnecessary output. |
02:51 | Then we give the location in the stream that we want to edit or display. |
02:57 | We want to select the second line. |
03:00 | p indicates the action we want to take i.e. to print the second line. |
03:06 | and seddemo.txt is the name of the file. |
03:11 | This is the general syntax of 'sed' command. |
03:15 | Now, let us print the last line of the file. |
03:20 | Let me clear the prompt. |
03:24 | Now, type: |
03:25 | sed space -n space within single quotes (dollar) $p after the single quotes space seddemo.txt |
03:36 | Press Enter. |
03:39 | We see that the last line is printed. |
03:42 | Now come back to text editor. |
03:45 | Suppose, we want to print the entries from 3rd to 6th. |
03:50 | For this, we need to type on the terminal: |
03:54 | sed space -n space within single quotes 3 (comma) ,6p’ space seddemo.txt |
04:07 | Press Enter. |
04:09 | The output is displayed from the third line to the sixth line. |
04:14 | Any of the actions can be reversed by using the exclamation mark (!) before the action. |
04:21 | Say, if we had to print all lines except from 3rd to 6th we will type:
sed space -n space within single quotes ‘3 (comma) ,6 (exclamation mark) !p |
04:38 | After the single quotes space seddemo.txt |
04:45 | Press Enter. |
04:46 | The output is displayed. |
04:49 | Let us switch back to our slides. |
04:52 | Line addressing and context addressing. |
04:56 | So far, we specified the lines in the file on which the action needs to be taken. |
05:02 | This is known as line addressing. |
05:05 | Address specified by the line numbers. |
05:08 | This is one way of addressing. |
05:11 | Another way of addressing is Context addressing |
05:16 | Lines that contain particular context say a particular word. |
05:21 | If we want to take actions on lines that contain a particular word, we use context addressing. |
05:29 | Regular expressions can be used. |
05:32 | Let us see an example. |
05:35 | Come back to our editor. |
05:38 | Say, we want to print those lines which have the word "computers". |
05:44 | Come back to our terminal. |
05:46 | Now, type: |
05:48 | sed space -n space (within single quotes) (front slash)(opening square bracket) [cC] (Closing Square bracket) omputers/p after the single quotesspace seddemo.txt |
06:14 | Press Enter. |
06:16 | We see the lines with the word "computers" is displayed. |
06:22 | We write pattern within square-brackets. |
06:24 | This is to match any one or both of the characters within square-brackets. |
06:30 | When we need to match patterns, the pattern needs to be typed between front slashes. |
06:37 | We can print it in a file as well using the w option. |
06:43 | For this, type: |
06:44 | sed space -n space (within single quotes) (front slash) (opening square bracket) [cC] (closing square bracket)omputers/w space computer_student.txt after the single quotes space seddemo.txt |
07:11 | Press Enter. |
07:14 | Now all the matching lines would be transferred to the file computer_student.txt. |
07:21 | Let us view the contents. |
07:25 | Type: cat space computer_student.txt |
07:32 | Press Enter. |
07:35 | We see the entries. |
07:37 | We can also have patterns that we can write to different files. |
07:42 | Let us clear the prompt. |
07:45 | Type: sed space -n space -e space (within single quotes) (front slash) ‘/electronics/w space electro.txt’ after the single quotes space -e space (within single quotes) (front slash) ‘/civil/w space civil.txt’ after the single quote space seddemo.txt |
08:18 | Press Enter. |
08:22 | Here -e is used to combine multiple methods. |
08:27 | This would create two files- electro.txt and civil.txt. |
08:34 | To see what they contain, type: |
08:37 | cat space electro.txt |
08:42 | This will display the entries with the word "electronics". |
08:47 | Let us see the content of "civil" file. |
08:50 | Type: cat space civil.txt |
08:55 | press Enter. |
08:57 | This will display the entries having the word "civil". |
09:01 | We will see some more set of commands in another tutorial. |
09:05 | I will use the same program. |
09:08 | This brings us to the end of this tutorial. |
09:11 | Let us switch back to our slide. |
09:14 | we will summarize. |
09:15 | In this tutorial, we learnt: sed |
09:18 | * To print using sed. |
09:19 | * Line Addressing. |
09:21 | * Context Addressing. |
09:23 | As an assignment, |
09:25 | Use the same text file "seddemo.txt". |
09:28 | Try to print records from 6th to 12th line. |
09:33 | Watch the video available at the link shown below. |
09:36 | It summarizes the Spoken Tutorial project. |
09:39 | If you do not have good bandwidth, you can download and watch it. |
09:44 | The Spoken Tutorial project team: |
09:46 | Conducts workshops using spoken tutorials. |
09:49 | Gives certificates to those who pass an online test. |
09:53 | For more details, please write to contact@spoken-tutorial.org |
10:00 | Spoken Tutorial project is a part of the Talk to a Teacher project. |
10:04 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
10:11 | More information on this mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
10:17 | This is Ashwini Patil from IIT Bombay, signing off. Thank You for joining. |