1 00:00:01 --> 00:00:05 Welcome to the spoken tutorial on More on sed. 2 00:00:06 --> 00:00:12 In this tutorial, we will learn some more on sed commands through some examples. 3 00:00:13 --> 00:00:14 To record this tutorial, I am using 4 00:00:15 --> 00:00:19 *Ubuntu Linux version 12.04 Operating System and 5 00:00:20 --> 00:00:23 *GNU BASH version 4.2.24 6 00:00:24 --> 00:00:31 Please note, GNU Bash version 4 or above is recommended to practice this tutorial. 7 00:00:32 --> 00:00:33 As prerequisites, 8 00:00:34 --> 00:00:36 You should know basics of Linux terminal. 9 00:00:37 --> 00:00:39 You should be aware of sed tool. 10 00:00:40 --> 00:00:45 For relevant tutorials, please visit our website which as shown: http://spoken-tutorial.org 11 00:00:46 --> 00:00:48 The major use of sed is substitution. 12 00:00:49 --> 00:00:54 Replacing some pattern in the input with something else. 13 00:00:55 --> 00:01:00 Let's first look at the original file seddemo.txt. 14 00:01:01 --> 00:01:09 Notice that the word "Kumar" occurs two times in the fourth line and once in the sixth line. 15 00:01:10 --> 00:01:15 If you want to replace all occurrences of "Kumar" with "Roy", 16 00:01:16 --> 00:01:17 type on the terminal: 17 00:01:18 --> 00:01:39 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 18 00:01:40 --> 00:01:42 Press Enter. 19 00:01:43 --> 00:01:45 Notice the fourth line. 20 00:01:46 --> 00:01:51 Only the first occurrence of "Kumar" is changed to "Roy", not the second one. 21 00:01:52 --> 00:01:56 Sixth line had one occurrence of the word "Kumar" and this is replaced now. 22 00:01:57 --> 00:02:02 So, we see only the first entry of the lines has been changed. 23 00:02:03 --> 00:02:10 This is because, by default, the first matched entry of any line is substituted. 24 00:02:11 --> 00:02:16 To substitute all the matched entries we need to use the flag g option. 25 00:02:17 --> 00:02:19 Let me clear the prompt. 26 00:02:20 --> 00:02:42 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. 27 00:02:43 --> 00:02:47 Now, both entries of the fourth line are replaced. 28 00:02:48 --> 00:02:52 We can make multiple substitutions in one go as well. 29 00:02:53 --> 00:02:57 Say, we want to replace the word "electronics" with the word "electrical" 30 00:02:58 --> 00:03:03 and "civil" with "metallurgy" in the file seddemo.txt. 31 00:03:04 --> 00:03:06 Let me clear the prompt 32 00:03:07 --> 00:03:36 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 33 00:03:37 --> 00:03:38 Press Enter. 34 00:03:39 --> 00:03:42 You can see that the words are replaced. 35 00:03:43 --> 00:03:48 Now we want to change the stream of 'Anirban' from "computers" to "mathematics." 36 00:03:49 --> 00:03:53 In such case we would need to type: 37 00:03:54 --> 00:04:10 sed space within single quotes 'front-slash Anirban slash s slash computers slash mathematics slash g' after the single quotes space seddemo.txt 38 00:04:11 --> 00:04:13 Press Enter. 39 00:04:14 --> 00:04:16 We see that the stream is changed. 40 00:04:17 --> 00:04:20 Let us understand what this is. 41 00:04:21 --> 00:04:27 First we write sed, then in single quotes we write the pattern to be matched. 42 00:04:28 --> 00:04:29 This is "Anirban". 43 00:04:30 --> 00:04:33 Now after the slash comes the operation. 44 00:04:34 --> 00:04:40 This is 's' that stands for substitution as we have already seen. 45 00:04:41 --> 00:04:46 Then we mention the pattern to be replaced which is "computers". 46 00:04:47 --> 00:04:52 Then the new word that would be substituted which is "mathematics". 47 00:04:53 --> 00:04:59 We can also use sed to add or delete lines in the file. 48 00:05:00 --> 00:05:05 Say, we want to select those lines which do not have stream as "electronics." 49 00:05:06 --> 00:05:09 For that, we have the d flag. 50 00:05:10 --> 00:05:30 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 51 00:05:31 --> 00:05:32 Press Enter. 52 00:05:33 --> 00:05:42 To see the contents, type: cat space nonelectronics.txt. 53 00:05:43 --> 00:05:48 Say, at the start of the file we want to add a line Student Information. 54 00:05:49 --> 00:05:53 For that we have the i action. 55 00:05:54 --> 00:06:09 We need to type: sed space in single quotes '1i space Student Information' after the quote space seddemo.txt 56 00:06:10 --> 00:06:12 And press Enter. 57 00:06:13 --> 00:06:14 You can see the output. 58 00:06:15 --> 00:06:19 In fact, we can enter multiple lines like this. 59 00:06:20 --> 00:06:25 Say, we want to add the two lines. We would do it in a similar way. 60 00:06:26 --> 00:06:32 Along with Student Information we also want to add academics in the next year. 61 00:06:33 --> 00:06:54 So in that case we would write sed space in single quotes 1i space Student Information slash n 2013 after the quotes seddemo.txt 62 00:06:55 --> 00:06:56 Press Enter. 63 00:06:57 --> 00:07:04 Notice slash n between the string ‘Information’ and ‘2013’. 64 00:07:05 --> 00:07:11 slash n prints 2013 in the next line after ‘Student Information’. 65 00:07:12 --> 00:07:13 This brings us to the end of this tutorial. 66 00:07:14 --> 00:07:16 Let us summarize, 67 00:07:17 --> 00:07:18 In this tutorial we learnt: 68 00:07:19 --> 00:07:19 * Substitution 69 00:07:20 --> 00:07:20 * Replacement 70 00:07:21 --> 00:07:23 * And Insertion. 71 00:07:24 --> 00:07:29 As an assignment, use the same text file seddemo.txt 72 00:07:30 --> 00:07:34 and try to replace or substitute name "Ankit" with "Ashish". 73 00:07:35 --> 00:07:38 Watch the video available at the link shown below http://spoken-tutorial.org/What_is_a_Spoken Tutorial 74 00:07:39 --> 00:07:41 It summarizes the Spoken Tutorial project. 75 00:07:42 --> 00:07:46 If you do not have good bandwidth, you can download and watch it. 76 00:07:47 --> 00:07:52 The Spoken Tutorial Project Team: Conducts workshops using spoken tutorials. 77 00:07:53 --> 00:07:56 Gives certificates to those who pass an online test. 78 00:07:57 --> 00:08:03 For more details, please write to contact@spoken-tutorial.org 79 00:08:04 --> 00:08:08 Spoken Tutorial Project is a part of the Talk to a Teacher project. 80 00:08:09 --> 00:08:15 It is supported by the National Mission on Education through ICT, MHRD, Government of India. 81 00:08:16 --> 00:08:21 More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro. 82 00:08:22 --> 00:08:27 The script has been contributed by Anirban and Sachin. 83 00:08:28 --> 00:08:33 This is Ashwini PAtil from IIT Bombay, signing off. Thank you for joining.