Difference between revisions of "Linux/C3/The-sed-command/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
|||
(15 intermediate revisions by 3 users not shown) | |||
Line 5: | Line 5: | ||
|- | |- | ||
| 00:01 | | 00:01 | ||
− | | Welcome to this spoken tutorial on | + | | Welcome to this spoken tutorial on '''sed''' - the '''stream editor'''. |
|- | |- | ||
| 00:05 | | 00:05 | ||
− | | In this tutorial | + | | In this tutorial, we will learn usage of '''sed '''command. |
|- | |- | ||
| 00:11 | | 00:11 | ||
− | |We will do through some examples | + | |We will do this through some examples. |
|- | |- | ||
Line 21: | Line 21: | ||
|- | |- | ||
| 00:16 | | 00:16 | ||
− | |I am using'''Ubuntu Linux''' version 12.04''' Operating System''' and | + | |I am using '''Ubuntu Linux''' version 12.04''' Operating System''' and '''GNU BASH''' version 4.2.24 |
− | '''GNU BASH''' version 4.2.24 | + | |
|- | |- | ||
Line 30: | Line 29: | ||
|- | |- | ||
| 00:34 | | 00:34 | ||
− | | As prerequisites | + | | As prerequisites, |
|- | |- | ||
| 00:36 | | 00:36 | ||
− | | | + | |you should know basics of Linux terminal. |
|- | |- | ||
| 00:39 | | 00:39 | ||
− | |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:45 | | 00:45 | ||
− | | Let us with an introduction to '''sed''' | + | | Let us start with an introduction to '''sed''': |
|- | |- | ||
|00:48 | |00:48 | ||
− | |'''sed''' is a stream editor. | + | |'''sed''' is a '''stream editor'''. |
− | + | ||
|- | |- | ||
Line 55: | Line 53: | ||
|- | |- | ||
| 00:58 | | 00:58 | ||
− | |It performs some display or editing function | + | |It performs some display or editing function- |
|- | |- | ||
Line 63: | Line 61: | ||
|- | |- | ||
| 01:10 | | 01:10 | ||
− | | Let us | + | | Let us start with some examples. |
|- | |- | ||
Line 71: | Line 69: | ||
|- | |- | ||
| 01:19 | | 01:19 | ||
− | |I have a file called '''seddemo.txt''' in home directory | + | |I have a file called '''seddemo.txt''' in '''home directory''' version. |
− | version | + | |
− | + | |- | |
− | + | | 01:24 | |
− | + | |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:39 | | 01:39 | ||
− | |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:44 | | 01:44 | ||
− | |For this we need to | + | |For this, we need to open the terminal by pressing '''Ctrl + Alt''' and '''T''' keys simultaneously on your keyboard. |
− | + | ||
|- | |- | ||
| 01:53 | | 01:53 | ||
− | |Now | + | |Now type: |
|- | |- | ||
Line 101: | Line 97: | ||
|- | |- | ||
| 02:03 | | 02:03 | ||
− | |''' | + | |Press '''Enter'''. |
|- | |- | ||
| 02:06 | | 02:06 | ||
− | |Here '''2''' denotes the location which is the second line. | + | |Here, '''2''' denotes the location which is the second line. |
|- | |- | ||
Line 121: | Line 117: | ||
|- | |- | ||
| 02:25 | | 02:25 | ||
− | |This is the default | + | |This is the default behavior of the action '''p'''. |
|- | |- | ||
| 02:29 | | 02:29 | ||
− | |To only print the second line | + | |To only print the second line, |
|- | |- | ||
| 02:31 | | 02:31 | ||
− | | | + | |type: |
|- | |- | ||
| 02:33 | | 02:33 | ||
− | |'''sed '''space '''-n''' space (Within single quotes) '''2p''' after the single quotes space '''seddemo.txt ''' | + | |'''sed''' space '''-n''' space (Within single quotes) '''2p''' after the single quotes space '''seddemo.txt ''' |
|- | |- | ||
| 02:44 | | 02:44 | ||
− | |''' | + | |Press '''Enter.''' |
|- | |- | ||
Line 149: | Line 145: | ||
|- | |- | ||
| 02:58 | | 02:58 | ||
− | |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 157: | Line 153: | ||
|- | |- | ||
| 03:07 | | 03:07 | ||
− | |'''p''' indicates the action we want to take | + | |'''p''' indicates the action we want to take i.e to print the second line. |
− | + | ||
|- | |- | ||
| 03:12 | | 03:12 | ||
− | |And '''seddemo.txt''' is the name of the file | + | |And '''seddemo.txt''' is the name of the file. |
|- | |- | ||
| 03:18 | | 03:18 | ||
− | |This is the general syntax of sed command. | + | |This is the general syntax of '''sed command'''. |
|- | |- | ||
Line 174: | Line 169: | ||
|- | |- | ||
|03:26 | |03:26 | ||
− | |Let me clear the | + | |Let me clear the prompt. |
|- | |- | ||
| 03:29 | | 03:29 | ||
− | |Now | + | |Now type: |
|- | |- | ||
Line 186: | Line 181: | ||
|- | |- | ||
| 03:42 | | 03:42 | ||
− | |Press '''Enter'''. | + | |Press '''Enter'''.We see that the last line is printed. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 03:49 | | 03:49 | ||
− | |Now come back to text editor. | + | |Now come back to our text editor. |
|- | |- | ||
| 03:51 | | 03:51 | ||
− | | Suppose we want to print the | + | | Suppose we want to print the entries from 3rd to 6th. |
− | + | ||
− | + | ||
|- | |- | ||
| 03:57 | | 03:57 | ||
− | |For this we need to type on the terminal: | + | |For this, we need to type on the terminal: |
|- | |- | ||
Line 212: | Line 201: | ||
|- | |- | ||
| 04:14 | | 04:14 | ||
− | |''' | + | |Press '''Enter.''' |
|- | |- | ||
Line 224: | Line 213: | ||
|- | |- | ||
| 04:28 | | 04:28 | ||
− | |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''' | ||
|- | |- | ||
| 04:44 | | 04:44 | ||
− | | | + | |after the single quotes space '''seddemo.txt ''' |
|- | |- | ||
| 04:51 | | 04:51 | ||
− | | | + | |Press '''Enter.''' |
|- | |- | ||
Line 245: | Line 234: | ||
|- | |- | ||
| 04:58 | | 04:58 | ||
− | |Line addressing and context addressing. | + | |'''Line addressing''' and '''context addressing'''. |
|- | |- | ||
| 05:03 | | 05:03 | ||
| So far, we specified the lines in the file on which the action needs to be taken. | | So far, we specified the lines in the file on which the action needs to be taken. | ||
− | |||
− | |||
|- | |- | ||
| 05:09 | | 05:09 | ||
− | |This is known as line addressing | + | |This is known as ''line addressing''- |
|- | |- | ||
| 05:12 | | 05:12 | ||
− | | | + | |address specified by the line numbers. |
|- | |- | ||
Line 267: | Line 254: | ||
|- | |- | ||
| 05:18 | | 05:18 | ||
− | |Another way of addressing is | + | |Another way of addressing is ''Context addressing''- |
|- | |- | ||
| 05:22 | | 05:22 | ||
− | | | + | |lines that contain particular context say a particular word. |
|- | |- | ||
| 05:28 | | 05:28 | ||
− | |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''. |
|- | |- | ||
Line 287: | Line 274: | ||
|- | |- | ||
| 05:42 | | 05:42 | ||
− | |Come back to our editor. | + | |Come back to our text editor. |
|- | |- | ||
| 05:44 | | 05:44 | ||
− | | Say we want to print those lines which have the word '''computers'''. | + | | Say, we want to print those lines which have the word '''computers'''. |
|- | |- | ||
| 05:50 | | 05:50 | ||
− | |Come back to our | + | |Come back to our terminal. |
|- | |- | ||
| 05:53 | | 05:53 | ||
− | |Now | + | |Now type:'''sed''' space '''-n''' space within single quotes front slash (opening square bracket) [cC] (Closing Square bracket) omputers/p after the single quotes'''space '''seddemo.txt ''' |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 06:20 | | 06:20 | ||
− | | | + | |Press '''Enter.''' |
|- | |- | ||
| 06:23 | | 06:23 | ||
− | |We see the lines with the word computers is displayed. | + | |We see the lines with the word "computers" is displayed. |
|- | |- | ||
Line 325: | Line 306: | ||
|- | |- | ||
| 06:36 | | 06:36 | ||
− | |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:43 | | 06:43 | ||
− | | 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. |
|- | |- | ||
Line 337: | Line 318: | ||
|- | |- | ||
| 06:52 | | 06:52 | ||
− | |'''sed''' space '''-n''' space | + | |'''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:18 | | 07:18 | ||
− | | | + | |Press '''Enter.''' |
|- | |- | ||
Line 353: | Line 334: | ||
|- | |- | ||
| 07:31 | | 07:31 | ||
− | | Type '''cat''' space '''computer_student''' | + | | Type: '''cat''' space '''computer_student.txt''' |
|- | |- | ||
| 07:38 | | 07:38 | ||
− | |''' | + | |Press '''Enter.''' |
|- | |- | ||
| 07:42 | | 07:42 | ||
− | |We see the entries | + | |We see the entries.We can also have patterns that we can write to different files. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 07:50 | | 07:50 | ||
− | | Let us clear the | + | | Let us clear the prompt. |
|- | |- | ||
| 07:52 | | 07:52 | ||
− | | Type '''sed''' space '''-n''' space '''-e''' space (within single quotes) (front slash) '''‘/electronics/w''' space '''electro.txt’''' after the single quotes | + | | 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 ''' |
|- | |- | ||
Line 381: | Line 358: | ||
|- | |- | ||
| 08:28 | | 08:28 | ||
− | |Here '''-e''' is used to combine multiple methods | + | |Here '''-e''' is used to combine multiple methods. |
|- | |- | ||
Line 389: | Line 366: | ||
|- | |- | ||
|08:41 | |08:41 | ||
− | | To see what they contain type: | + | | To see what they contain, type: |
|- | |- | ||
Line 397: | Line 374: | ||
|- | |- | ||
| 08:49 | | 08:49 | ||
− | |This will display the | + | |This will display the entries with the word "electronics". |
− | + | ||
|- | |- | ||
| 08:54 | | 08:54 | ||
− | |Let us see the | + | |Let us see the contents of civil file. |
|- | |- | ||
| 08:58 | | 08:58 | ||
− | | Type '''cat '''space''' civil.txt''' | + | | Type: '''cat''' space ''' civil.txt''' |
|- | |- | ||
| 09:01 | | 09:01 | ||
− | |''' | + | | press '''Enter'''. |
|- | |- | ||
| 09:03 | | 09:03 | ||
− | |This will display the | + | |This will display the entries having the word "civil". |
|- | |- | ||
Line 430: | Line 406: | ||
|- | |- | ||
|09:18 | |09:18 | ||
− | |Let us switch back to our slide | + | |Let us switch back to our slide. |
|- | |- | ||
Line 438: | Line 414: | ||
|- | |- | ||
| 09:22 | | 09:22 | ||
− | |In this tutorial we learnt, | + | |In this tutorial we learnt, '''sed''': |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 09:25 | | 09:25 | ||
− | |To print using sed | + | | To print using '''sed''', '''Line Addressing''', |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 09:27 | | 09:27 | ||
− | |Context Addressing. | + | | '''Context Addressing'''. |
|- | |- | ||
Line 464: | Line 430: | ||
|- | |- | ||
| 09:32 | | 09:32 | ||
− | | | + | |use the same text file '''seddemo.txt''', |
|- | |- | ||
| 09:35 | | 09:35 | ||
− | | | + | |try to print records from 6th to 12th line. |
|- | |- | ||
| 09:40 | | 09:40 | ||
− | | Watch the video available at the link shown below | + | | Watch the video available at the link shown below. |
|- | |- | ||
| 09:42 | | 09:42 | ||
− | |It | + | |It summarizes the Spoken Tutorial project. |
|- | |- | ||
| 09:46 | | 09:46 | ||
− | |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:51 | | 09:51 | ||
− | | The Spoken Tutorial Project Team | + | | The Spoken Tutorial Project Team: |
|- | |- | ||
Line 492: | Line 458: | ||
|- | |- | ||
| 09:55 | | 09:55 | ||
− | |Gives certificates to those who pass an online test | + | |Gives certificates to those who pass an online test. |
|- | |- | ||
| 10:00 | | 10:00 | ||
− | |For more details, please write tocontact@spoken-tutorial.org | + | |For more details, please write tocontact@spoken-tutorial.org. |
|- | |- | ||
| 10:07 | | 10:07 | ||
− | | Spoken Tutorial Project is a part of the Talk to a Teacher project | + | | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
|- | |- | ||
| 10:11 | | 10:11 | ||
− | |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:18 | | 10:18 | ||
− | |More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro | + | |More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro. |
|- | |- | ||
| 10:25 | | 10:25 | ||
− | |This is Ashwini from IIT Bombay signing off . Thank You for joining. | + | |This is Ashwini Patil from IIT Bombay, signing off . Thank You for joining. |
|} | |} |
Latest revision as of 15:17, 24 August 2017
Time | Narration |
00:01 | Welcome to this spoken tutorial on sed - the stream editor. |
00:05 | In this tutorial, we will learn usage of sed command. |
00:11 | We will do this through some examples. |
00:14 | To record this tutorial, |
00:16 | I am using Ubuntu Linux version 12.04 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:34 | As prerequisites, |
00:36 | you should know basics of Linux terminal. |
00:39 | For relevant tutorials please visit our website which is as shown: http://spoken-tutorial.org |
00:45 | Let us start with an introduction to sed: |
00:48 | sed is a stream editor. |
00:51 | sed finds some pattern of text in a particular location of a file. |
00:58 | It performs some display or editing function- |
01:02 | editing function like insertion, substitution and deletion in matched text. |
01:10 | Let us start with some examples. |
01:13 | We will see how to print using the sed command. |
01:19 | I have a file called seddemo.txt in home directory version. |
01:24 | 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:39 | Now, suppose we want to print the second line of the file. |
01:44 | For this, we need to open the terminal by pressing Ctrl + Alt and T keys simultaneously on your keyboard. |
01:53 | Now type: |
01:55 | sed space within single quotes ‘2p’ after the single quotes space seddemo.txt |
02:03 | Press Enter. |
02:06 | Here, 2 denotes the location which is the second line. |
02:11 | p denotes the action, which is printing(p). |
02:16 | Now Look at the output. |
02:18 | It shows the entire file but see that the second line is printed twice. |
02:25 | This is the default behavior of the action p. |
02:29 | To only print the second line, |
02:31 | type: |
02:33 | sed space -n space (Within single quotes) 2p after the single quotes space seddemo.txt |
02:44 | Press Enter. |
02:46 | We see only the second line as printed. |
02:51 | -n stands for ‘silent mode’ which will suppress all unnecessary output. |
02:58 | Then we give the location in the stream that we want to edit or display. |
03:03 | We want to select the second line. |
03:07 | p indicates the action we want to take i.e to print the second line. |
03:12 | And seddemo.txt is the name of the file. |
03:18 | This is the general syntax of sed command. |
03:21 | Now let us print the last line of the file. |
03:26 | Let me clear the prompt. |
03:29 | Now type: |
03:32 | sed space -n space within single quotes (dollar) $p after the single quotes space seddemo.txt |
03:42 | Press Enter.We see that the last line is printed. |
03:49 | Now come back to our text editor. |
03:51 | Suppose we want to print the entries from 3rd to 6th. |
03:57 | For this, we need to type on the terminal: |
04:00 | sed space -n space within single quotes 3 (comma) ,6p’ space seddemo.txt |
04:14 | Press Enter. |
04:16 | The output is displayed from the third line to the sixth line. |
04:21 | Any of the actions can be reversed by using the exclamation mark before the action. |
04:28 | 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:44 | after the single quotes space seddemo.txt |
04:51 | Press Enter. |
04:53 | The output is displayed. |
04:56 | Let us switch back to our slides. |
04:58 | Line addressing and context addressing. |
05:03 | So far, we specified the lines in the file on which the action needs to be taken. |
05:09 | This is known as line addressing- |
05:12 | address specified by the line numbers. |
05:15 | This is one way of addressing. |
05:18 | Another way of addressing is Context addressing- |
05:22 | lines that contain particular context say a particular word. |
05:28 | If we want to take actions on lines that contain a particular word, we use context addressing. |
05:36 | Regular expressions can be used. |
05:39 | Let us see an example. |
05:42 | Come back to our text editor. |
05:44 | Say, we want to print those lines which have the word computers. |
05:50 | Come back to our terminal. |
05:53 | Now type: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:20 | Press Enter. |
06:23 | We see the lines with the word "computers" is displayed. |
06:28 | We write pattern within square brackets. |
06:31 | This is to match any one or both of the characters within square brackets. |
06:36 | When we need to match patterns, the pattern needs to be typed between front slashes. |
06:43 | We can print it in a file as well, using the w option. |
06:50 | For this type: |
06:52 | 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:18 | Press Enter. |
07:21 | Now all the matching lines would be transferred to the file computer_student.txt. |
07:27 | Let us view the content of computer_student |
07:31 | Type: cat space computer_student.txt |
07:38 | Press Enter. |
07:42 | We see the entries.We can also have patterns that we can write to different files. |
07:50 | Let us clear the prompt. |
07:52 | 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:24 | Press Enter |
08:28 | Here -e is used to combine multiple methods. |
08:34 | This would create two files electro.txt and civil.txt. |
08:41 | To see what they contain, type: |
08:43 | cat space electro.txt |
08:49 | This will display the entries with the word "electronics". |
08:54 | Let us see the contents of civil file. |
08:58 | Type: cat space civil.txt |
09:01 | press Enter. |
09:03 | This will display the entries having the word "civil". |
09:08 | We will see some more set of commands in another tutorial. |
09:12 | I will use the same program. |
09:14 | This brings us to the end of this tutorial. |
09:18 | Let us switch back to our slide. |
09:20 | we will summarize, |
09:22 | In this tutorial we learnt, sed: |
09:25 | To print using sed, Line Addressing, |
09:27 | Context Addressing. |
09:30 | As an assignment, |
09:32 | use the same text file seddemo.txt, |
09:35 | try to print records from 6th to 12th line. |
09:40 | Watch the video available at the link shown below. |
09:42 | It summarizes the Spoken Tutorial project. |
09:46 | If you do not have good bandwidth, you can download and watch it. |
09:51 | The Spoken Tutorial Project Team: |
09:53 | Conducts workshops using spoken tutorials |
09:55 | Gives certificates to those who pass an online test. |
10:00 | For more details, please write tocontact@spoken-tutorial.org. |
10:07 | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
10:11 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
10:18 | More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro. |
10:25 | This is Ashwini Patil from IIT Bombay, signing off . Thank You for joining. |