1 00:00:05,901 --> 00:00:09,630 - In this video, we are going to explore sed. 2 00:00:09,630 --> 00:00:10,981 We have just talked about awk, 3 00:00:10,981 --> 00:00:12,781 and you have learned that awk is useful 4 00:00:12,781 --> 00:00:15,731 for filtering text and printing specific values only 5 00:00:15,731 --> 00:00:19,110 sed is a stream editor, and it allows 6 00:00:19,110 --> 00:00:21,870 you to edit files in a non-visual mode. 7 00:00:21,870 --> 00:00:24,432 So it's really a programmable editor, 8 00:00:24,432 --> 00:00:27,930 and let's just have a look at a couple of examples with sed. 9 00:00:27,930 --> 00:00:30,330 Let me pop the examples on the slide to make it easy 10 00:00:30,330 --> 00:00:32,040 for you to repeat them yourself. 11 00:00:32,040 --> 00:00:33,543 And let me just demonstrate. 12 00:00:35,520 --> 00:00:40,520 So, to start with sed minus N five P on etc Pass WD, 13 00:00:43,380 --> 00:00:46,590 which is doing what? This is printing line five, and 14 00:00:46,590 --> 00:00:50,280 if you don't believe me, well, let's do a head minus five 15 00:00:50,280 --> 00:00:55,280 etc Pass WD pipe till minus one. 16 00:00:56,221 --> 00:00:58,920 Which is doing the exact same thing. 17 00:00:58,920 --> 00:01:00,270 Line five. 18 00:01:00,270 --> 00:01:03,090 Good. Now, one thing that you cannot do so easily 19 00:01:03,090 --> 00:01:05,670 with other utilities from the command line is 20 00:01:05,670 --> 00:01:08,343 the following, sed minus I. 21 00:01:09,420 --> 00:01:11,640 We need a minus I to make sure that the 22 00:01:11,640 --> 00:01:14,370 manipulation is written to the target fell and 23 00:01:14,370 --> 00:01:19,170 not printed on the standard out. S which is substitute. 24 00:01:19,170 --> 00:01:24,170 And I am going to use substitute Anna for Bob, 25 00:01:25,200 --> 00:01:29,340 and I'm making that a global substitute on users. 26 00:01:29,340 --> 00:01:30,900 And what is the result? 27 00:01:30,900 --> 00:01:33,032 Well, we can see the result 28 00:01:33,032 --> 00:01:38,032 in the users fell everywhere, where we had Anna before. 29 00:01:38,850 --> 00:01:43,440 Now we have Bob. Notice, there is no easy way to undo that. 30 00:01:43,440 --> 00:01:46,020 So make sure that you know, what you are doing. 31 00:01:46,020 --> 00:01:50,962 Also convenient, is the option to use sed, to 32 00:01:50,962 --> 00:01:53,293 to delete entries from a file. 33 00:01:53,293 --> 00:01:54,720 It may happen that 34 00:01:54,720 --> 00:01:57,060 while you are doing whatever manipulation 35 00:01:57,060 --> 00:02:00,600 you get an error, on a line, in a file. 36 00:02:00,600 --> 00:02:03,120 And if you identify the line number 37 00:02:03,120 --> 00:02:06,930 then you can use sed minus I, where the minus I 38 00:02:06,930 --> 00:02:11,310 is writing the manipulation directly to the file minus E 39 00:02:11,310 --> 00:02:12,780 which is an added command. 40 00:02:12,780 --> 00:02:15,390 And the added command is two D. Two is the line 41 00:02:15,390 --> 00:02:17,100 number, D is delete. 42 00:02:17,100 --> 00:02:18,450 And we do that on users. 43 00:02:18,450 --> 00:02:19,770 And what do we get? 44 00:02:19,770 --> 00:02:22,140 Well, as you can see, the second line, which had 45 00:02:22,140 --> 00:02:26,130 Bob belle has been deleted, that doesn't make sense anyway 46 00:02:26,130 --> 00:02:28,080 to have a Bob belle line. 47 00:02:28,080 --> 00:02:32,490 Now you can also use sed, in a shell loop. 48 00:02:32,490 --> 00:02:36,747 So, let me create a couple of files, echo, hello 49 00:02:36,747 --> 00:02:41,747 file one TXT, and I need to do a redirect. 50 00:02:43,920 --> 00:02:46,080 This is something we haven't seen before. 51 00:02:46,080 --> 00:02:48,000 Echo, hello, normally prints a text. 52 00:02:48,000 --> 00:02:48,833 Hello, on screen. 53 00:02:48,833 --> 00:02:50,280 I don't wanna have the text. 54 00:02:50,280 --> 00:02:51,150 Hello, on screen. 55 00:02:51,150 --> 00:02:52,915 I want to redirect it to a file. 56 00:02:52,915 --> 00:02:55,620 That's why I use the redirect, which means 57 00:02:55,620 --> 00:02:58,980 that the output of the command is written to the file, 58 00:02:58,980 --> 00:03:01,830 and I'm specifying right here. 59 00:03:01,830 --> 00:03:06,000 And I'm just creating a couple of files, four of them. 60 00:03:06,000 --> 00:03:07,140 That's pretty dumb. 61 00:03:07,140 --> 00:03:09,780 But what I want to show you is that sed is 62 00:03:09,780 --> 00:03:14,780 particularly useful in a small looping structure for I 63 00:03:15,270 --> 00:03:20,270 in star TXT do sed minus I slash minus I S 64 00:03:25,350 --> 00:03:30,350 slash hello slash bye slash G single quote dollar I Don. 65 00:03:36,930 --> 00:03:38,010 So what is that? 66 00:03:38,010 --> 00:03:41,550 Well, this is actually a very simple shell script, 67 00:03:41,550 --> 00:03:43,020 a short iteration. 68 00:03:43,020 --> 00:03:44,580 The first part of the iteration 69 00:03:44,580 --> 00:03:48,390 for I in start TxT really means 70 00:03:48,390 --> 00:03:52,565 for each element that matches the pattern, start dot TxT. 71 00:03:52,565 --> 00:03:55,680 We are going to treat them one by one. 72 00:03:55,680 --> 00:03:56,790 And how do we do that? 73 00:03:56,790 --> 00:03:58,500 Well, we do that with the due part. 74 00:03:58,500 --> 00:04:01,620 And in the due part, we are using a set 75 00:04:01,620 --> 00:04:06,000 minus I that's a substitute operation on dollar I, dollar 76 00:04:06,000 --> 00:04:08,880 I is the name of the file in the current iteration, 77 00:04:08,880 --> 00:04:11,940 and don't is closing the iteration loop. 78 00:04:11,940 --> 00:04:16,940 And if I would use cat on file one dot TXT there, we can see 79 00:04:17,307 --> 00:04:21,785 that the substitution has been executed the right way. 80 00:04:21,785 --> 00:04:25,230 This is a small shelf scripting iteration. 81 00:04:25,230 --> 00:04:29,340 Shelf scripting is a powerful solution in Linux as well. 82 00:04:29,340 --> 00:04:31,740 Not something that we are going to explore in discourse, 83 00:04:31,740 --> 00:04:33,840 but I definitely recommend that 84 00:04:33,840 --> 00:04:35,370 the next thing that you are going to learn 85 00:04:35,370 --> 00:04:38,880 about Linux to become a Linux power user, is shell 86 00:04:38,880 --> 00:04:42,903 scripting's, pretty important part of your Linux toolbox.