Difference between revisions of "Linux/C3/More-on-sed-command/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
(2 intermediate revisions by 2 users not shown) | |||
Line 10: | Line 10: | ||
|- | |- | ||
| 00:06 | | 00:06 | ||
− | | In this tutorial, we will learn some ''' | + | | In this tutorial, we will learn some more on '''sed commands''' through some examples. |
|- | |- | ||
Line 18: | Line 18: | ||
|- | |- | ||
| 00:15 | | 00:15 | ||
− | | | + | | '''Ubuntu Linux''' version 12.04''' Operating System''' and |
|- | |- | ||
| 00:20 | | 00:20 | ||
− | | | + | | '''GNU BASH''' version 4.2.24 |
|- | |- | ||
Line 38: | Line 38: | ||
|- | |- | ||
| 00:37 | | 00:37 | ||
− | | You should be aware of sed tool. | + | | You should be aware of '''sed''' tool. |
|- | |- | ||
| 00:40 | | 00:40 | ||
− | | For relevant tutorials please visit our website which as shown: http://spoken-tutorial.org | + | | For relevant tutorials, please visit our website which as shown: http://spoken-tutorial.org |
|- | |- | ||
Line 54: | Line 54: | ||
|- | |- | ||
| 00:55 | | 00:55 | ||
− | | Let's first look at the original file '''seddemo.txt''' | + | | Let's first look at the original file '''seddemo.txt'''. |
|- | |- | ||
| 01:01 | | 01:01 | ||
− | | Notice the word | + | | Notice that the word "Kumar" occurs two times in the fourth line and once in the sixth line. |
|- | |- | ||
| 01:10 | | 01:10 | ||
− | | If you want to replace all occurrences of | + | | If you want to replace all occurrences of "Kumar" with "Roy", |
|- | |- | ||
| 01:16 | | 01:16 | ||
− | | | + | |type on the '''terminal''': |
|- | |- | ||
| 01:18 | | 01:18 | ||
− | | '''sed space''' within single quote | + | | '''sed space''' within single quote '''s front slash / opening square bracket small k capital K closing square bracket umar slash Roy slash''' after the single quotes space '''seddemo.txt''' |
|- | |- | ||
Line 82: | Line 82: | ||
|- | |- | ||
| 01:46 | | 01:46 | ||
− | | Only the first occurrence of | + | | Only the first occurrence of "Kumar" is changed to "Roy", not the second one. |
|- | |- | ||
| 01:52 | | 01:52 | ||
− | | Sixth line had one occurrence of the word | + | | Sixth line had one occurrence of the word "Kumar" and this is replaced now. |
|- | |- | ||
Line 98: | Line 98: | ||
|- | |- | ||
| 02:11 | | 02:11 | ||
− | | To substitute all the matched entries we need to use the | + | | To substitute all the matched entries we need to use the '''flag g''' option. |
|- | |- | ||
| 02:17 | | 02:17 | ||
− | | Let me clear the | + | | Let me clear the prompt. |
|- | |- | ||
| 02:20 | | 02:20 | ||
− | | Type: | + | | Type: '''sed space''' (within single quotes)''' 's front-slash opening square bracket small k capital K closing square bracket umar slash Roy slash g' ''' after the single quote space '''seddemo.txt'''. Press '''Enter'''. |
− | '''sed space within single quotes 's front slash opening square bracket | + | |
|- | |- | ||
Line 119: | Line 118: | ||
|- | |- | ||
| 02:53 | | 02:53 | ||
− | | Say we want to replace the | + | | Say, we want to replace the word "electronics" with the word "electrical" |
|- | |- | ||
| 02:58 | | 02:58 | ||
− | | | + | | and "civil" with "metallurgy" in the file '''seddemo.txt'''. |
|- | |- | ||
Line 131: | Line 130: | ||
|- | |- | ||
| 03:07 | | 03:07 | ||
− | | Type: | + | | Type: '''sed space hyphen e space''' within single quotes '''‘s front slash electronics slash electrical slash g’ after the single quote space hyphen e space''' within single quotes ''' ‘s front-slash civil slash metallurgy slash g’ ''' after the single quotes space '''seddemo.txt''' |
− | + | ||
|- | |- | ||
| 03:37 | | 03:37 | ||
− | | | + | | Press '''Enter.''' |
|- | |- | ||
Line 144: | Line 142: | ||
|- | |- | ||
| 03:43 | | 03:43 | ||
− | | Now we want to change the '''stream''' of | + | | Now we want to change the '''stream''' of 'Anirban' from "computers" to "mathematics." |
|- | |- | ||
Line 152: | Line 150: | ||
|- | |- | ||
| 03:54 | | 03:54 | ||
− | | '''sed space''' within single quotes ''''front slash Anirban slash s slash computers slash mathematics slash g' ''' after the single quotes space '''seddemo.txt''' | + | | '''sed space''' within single quotes ''' 'front-slash Anirban slash s slash computers slash mathematics slash g' ''' after the single quotes space '''seddemo.txt''' |
|- | |- | ||
Line 168: | Line 166: | ||
|- | |- | ||
| 04:21 | | 04:21 | ||
− | | First we write '''sed''' then in single quotes we write the pattern to be matched. | + | | First we write '''sed''', then in single quotes we write the pattern to be matched. |
|- | |- | ||
| 04:28 | | 04:28 | ||
− | | This is | + | | This is "Anirban". |
|- | |- | ||
Line 180: | Line 178: | ||
|- | |- | ||
| 04:34 | | 04:34 | ||
− | | This is | + | | This is 's' that stands for substitution as we have already seen. |
|- | |- | ||
| 04:41 | | 04:41 | ||
− | | Then we mention the pattern to be replaced which is | + | | Then we mention the pattern to be replaced which is "computers". |
|- | |- | ||
| 04:47 | | 04:47 | ||
− | | Then the new word that would be substituted | + | | Then the new word that would be substituted which is "mathematics". |
|- | |- | ||
Line 196: | Line 194: | ||
|- | |- | ||
| 05:00 | | 05:00 | ||
− | | Say we want to select those lines which do not have '''stream''' as | + | | Say, we want to select those lines which do not have '''stream''' as "electronics." |
|- | |- | ||
| 05:06 | | 05:06 | ||
− | | For that, we have the '''d ''' | + | | For that, we have the '''d flag'''. |
|- | |- | ||
| 05:10 | | 05:10 | ||
− | | Type: | + | | Type: '''sed space''' within single quotes '''front-slash electronics slash d''' after the single quotes '''space seddemo.txt space greater than sign space nonelectronics.txt''' |
− | '''sed space''' within single quotes '''front slash electronics slash d''' after the single quotes '''space seddemo.txt space greater than sign space nonelectronics.txt''' | + | |
|- | |- | ||
Line 213: | Line 210: | ||
|- | |- | ||
| 05:33 | | 05:33 | ||
− | | To see the contents, type: '''cat space nonelectronics.txt''' | + | | To see the contents, type: '''cat space nonelectronics.txt'''. |
|- | |- | ||
Line 225: | Line 222: | ||
|- | |- | ||
| 05:54 | | 05:54 | ||
− | | We need to type: '''sed space''' in single quotes ''''1i space Student Information' after the quote seddemo.txt ''' | + | | We need to type: '''sed space''' in single quotes''' '1i space Student Information' after the quote space seddemo.txt ''' |
|- | |- | ||
Line 241: | Line 238: | ||
|- | |- | ||
| 06:20 | | 06:20 | ||
− | | Say, we want to add the two lines | + | | Say, we want to add the two lines. We would do it in a similar way. |
|- | |- | ||
Line 249: | Line 246: | ||
|- | |- | ||
| 06:33 | | 06:33 | ||
− | | So in that case we would write | + | | So in that case we would write '''sed space''' in single quotes '''1i space Student Information slash n 2013''' after the quotes '''seddemo.txt''' |
− | + | ||
|- | |- | ||
| 06:55 | | 06:55 | ||
− | | Press '''Enter''' | + | | Press '''Enter'''. |
|- | |- | ||
Line 262: | Line 258: | ||
|- | |- | ||
| 07:05 | | 07:05 | ||
− | | '''slash n '''prints 2013 in the next line after ‘Student Information’ | + | | '''slash n''' prints 2013 in the next line after ‘Student Information’. |
|- | |- | ||
| 07:12 | | 07:12 | ||
− | | This brings | + | | This brings us to the end of this tutorial. |
|- | |- | ||
Line 274: | Line 270: | ||
|- | |- | ||
| 07:17 | | 07:17 | ||
− | | In this tutorial we learnt | + | | In this tutorial we learnt: |
|- | |- | ||
| 07:19 | | 07:19 | ||
− | | Substitution | + | | Substitution Replacement |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 07:21 | | 07:21 | ||
− | | And Insertion | + | | And Insertion. |
|- | |- | ||
| 07:24 | | 07:24 | ||
− | | As an assignment, | + | | As an assignment, use the same text file '''seddemo.txt''' |
|- | |- | ||
| 07:30 | | 07:30 | ||
− | | | + | | and try to replace or substitute name "Ankit" with "Ashish". |
|- | |- | ||
Line 302: | Line 294: | ||
|- | |- | ||
| 07:39 | | 07:39 | ||
− | | It | + | | It summarizes the Spoken Tutorial project. |
|- | |- | ||
| 07:42 | | 07:42 | ||
− | | 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. |
|- | |- | ||
| 07:47 | | 07:47 | ||
− | | The Spoken Tutorial Project Team Conducts workshops using spoken tutorials | + | | The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials. |
|- | |- | ||
| 07:53 | | 07:53 | ||
− | | Gives certificates to those who pass an online test | + | | Gives certificates to those who pass an online test. |
|- | |- | ||
Line 322: | Line 314: | ||
|- | |- | ||
| 08:04 | | 08:04 | ||
− | | 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. |
|- | |- | ||
| 08:09 | | 08:09 | ||
− | | 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. |
|- | |- | ||
| 08:16 | | 08:16 | ||
− | | More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro | + | | More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro. |
|- | |- | ||
Line 338: | Line 330: | ||
|- | |- | ||
| 08:28 | | 08:28 | ||
− | |This is Ashwini PAtil from IIT | + | |This is Ashwini PAtil from IIT Bombay, signing off. |
Thank you for joining. | Thank you for joining. | ||
|} | |} |
Latest revision as of 18:01, 23 March 2017
Time | Narration |
00:01 | Welcome to the spoken tutorial on More on sed. |
00:06 | In this tutorial, we will learn some more on sed commands through some examples. |
00:13 | To record this tutorial, I am using |
00:15 | Ubuntu Linux version 12.04 Operating System and |
00:20 | GNU BASH version 4.2.24 |
00:24 | Please note, GNU Bash version 4 or above is recommended to practice this tutorial. |
00:32 | As prerequisites, |
00:34 | You should know basics of Linux terminal. |
00:37 | You should be aware of sed tool. |
00:40 | For relevant tutorials, please visit our website which as shown: http://spoken-tutorial.org |
00:46 | The major use of sed is substitution. |
00:49 | Replacing some pattern in the input with something else. |
00:55 | Let's first look at the original file seddemo.txt. |
01:01 | Notice that the word "Kumar" occurs two times in the fourth line and once in the sixth line. |
01:10 | If you want to replace all occurrences of "Kumar" with "Roy", |
01:16 | type on the terminal: |
01:18 | sed space within single quote s front slash / opening square bracket small k capital K closing square bracket umar slash Roy slash after the single quotes space seddemo.txt |
01:40 | Press Enter. |
01:43 | Notice the fourth line. |
01:46 | Only the first occurrence of "Kumar" is changed to "Roy", not the second one. |
01:52 | Sixth line had one occurrence of the word "Kumar" and this is replaced now. |
01:57 | So, we see only the first entry of the lines has been changed. |
02:03 | This is because, by default, the first matched entry of any line is substituted. |
02:11 | To substitute all the matched entries we need to use the flag g option. |
02:17 | Let me clear the prompt. |
02:20 | Type: sed space (within single quotes) 's front-slash opening square bracket small k capital K closing square bracket umar slash Roy slash g' after the single quote space seddemo.txt. Press Enter. |
02:43 | Now, both entries of the fourth line are replaced. |
02:48 | We can make multiple substitutions in one go as well. |
02:53 | Say, we want to replace the word "electronics" with the word "electrical" |
02:58 | and "civil" with "metallurgy" in the file seddemo.txt. |
03:04 | Let me clear the prompt |
03:07 | Type: sed space hyphen e space within single quotes ‘s front slash electronics slash electrical slash g’ after the single quote space hyphen e space within single quotes ‘s front-slash civil slash metallurgy slash g’ after the single quotes space seddemo.txt |
03:37 | Press Enter. |
03:39 | You can see that the words are replaced. |
03:43 | Now we want to change the stream of 'Anirban' from "computers" to "mathematics." |
03:49 | In such case we would need to type: |
03:54 | sed space within single quotes 'front-slash Anirban slash s slash computers slash mathematics slash g' after the single quotes space seddemo.txt |
04:11 | Press Enter. |
04:14 | We see that the stream is changed. |
04:17 | Let us understand what this is. |
04:21 | First we write sed, then in single quotes we write the pattern to be matched. |
04:28 | This is "Anirban". |
04:30 | Now after the slash comes the operation. |
04:34 | This is 's' that stands for substitution as we have already seen. |
04:41 | Then we mention the pattern to be replaced which is "computers". |
04:47 | Then the new word that would be substituted which is "mathematics". |
04:53 | We can also use sed to add or delete lines in the file. |
05:00 | Say, we want to select those lines which do not have stream as "electronics." |
05:06 | For that, we have the d flag. |
05:10 | Type: sed space within single quotes front-slash electronics slash d after the single quotes space seddemo.txt space greater than sign space nonelectronics.txt |
05:31 | Press Enter. |
05:33 | To see the contents, type: cat space nonelectronics.txt. |
05:43 | Say, at the start of the file we want to add a line Student Information. |
05:49 | For that we have the i action. |
05:54 | We need to type: sed space in single quotes '1i space Student Information' after the quote space seddemo.txt |
06:10 | And press Enter. |
06:13 | You can see the output. |
06:15 | In fact, we can enter multiple lines like this. |
06:20 | Say, we want to add the two lines. We would do it in a similar way. |
06:26 | Along with Student Information we also want to add academics in the next year. |
06:33 | So in that case we would write sed space in single quotes 1i space Student Information slash n 2013 after the quotes seddemo.txt |
06:55 | Press Enter. |
06:57 | Notice slash n between the string ‘Information’ and ‘2013’. |
07:05 | slash n prints 2013 in the next line after ‘Student Information’. |
07:12 | This brings us to the end of this tutorial. |
07:14 | Let us summarize, |
07:17 | In this tutorial we learnt: |
07:19 | Substitution Replacement |
07:21 | And Insertion. |
07:24 | As an assignment, use the same text file seddemo.txt |
07:30 | and try to replace or substitute name "Ankit" with "Ashish". |
07:35 | Watch the video available at the link shown below http://spoken-tutorial.org/What_is_a_Spoken Tutorial |
07:39 | It summarizes the Spoken Tutorial project. |
07:42 | If you do not have good bandwidth, you can download and watch it. |
07:47 | The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials. |
07:53 | Gives certificates to those who pass an online test. |
07:57 | For more details, please write to contact@spoken-tutorial.org |
08:04 | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
08:09 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
08:16 | More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro. |
08:22 | The script has been contributed by Anirban and Sachin. |
08:28 | This is Ashwini PAtil from IIT Bombay, signing off.
Thank you for joining. |