1
00:00:03,180 --> 00:00:08,760
In this lesson you'll learn how to use the most important features of the said Command said stands for

2
00:00:08,760 --> 00:00:12,690
stream Ed and its use for well editing streams.

3
00:00:12,690 --> 00:00:18,450
You might not be familiar with the term stream but if you performed in the IO redirection or piping

4
00:00:18,660 --> 00:00:24,900
you've been using streams you can think of a stream as the data that travels from one process to another

5
00:00:25,140 --> 00:00:33,870
through a pipe or from one file to another as a redirect or from one device to another so you can think

6
00:00:33,870 --> 00:00:40,950
of standard input as the standard input stream standard output as the standard output stream and standard

7
00:00:40,980 --> 00:00:43,330
error as the standard error stream.

8
00:00:43,470 --> 00:00:50,670
By the way these data streams are typically textual data the set command is used to perform basic text

9
00:00:50,670 --> 00:00:52,980
transformations on an input stream.

10
00:00:53,040 --> 00:00:59,640
For example you can use said to substitute some text for other text remove lines append text after given

11
00:00:59,640 --> 00:01:06,960
lines and insert text before certain lines said differs from other editors such as vim emacs and nano

12
00:01:06,960 --> 00:01:09,600
in that it's used programmatically.

13
00:01:09,660 --> 00:01:15,120
Of course you can substitute some text for other tax remove lines append texts after given lines and

14
00:01:15,120 --> 00:01:21,570
insert text before certain lines using them but that requires interaction from a human.

15
00:01:21,570 --> 00:01:27,060
Sure you can write macros to perform these functions in vim but you have to have someone to start them

16
00:01:27,390 --> 00:01:33,090
and then execute the macros which said you can programmatically perform these edits without the need

17
00:01:33,090 --> 00:01:33,980
of interaction.

18
00:01:34,330 --> 00:01:38,920
And this makes said ideal to be used in shell scripts for example.

19
00:01:39,030 --> 00:01:44,340
The most common use of said is to act as a command line version of Find and Replace.

20
00:01:44,760 --> 00:01:49,860
I'm going to use this particular application of said to teach you the basics of the said command.

21
00:01:49,860 --> 00:01:56,550
Notice that I said said Command said is a standalone utility and not a shell built in.

22
00:01:56,550 --> 00:01:59,430
We can prove this by using the type shell built in

23
00:02:02,530 --> 00:02:08,800
so you can see that when you type said in press enter the user been said program will be executed of

24
00:02:08,800 --> 00:02:15,110
course for standalone commands you can use man to get help and documentation on how to use that command.

25
00:02:15,130 --> 00:02:24,400
So let's quickly look at Said's man page as we've already mentioned said is a stream editor the man

26
00:02:24,400 --> 00:02:28,680
page says it's used for filtering and transforming text.

27
00:02:28,720 --> 00:02:34,540
If we look at the synopsis section of the man page we can see that we can execute said with zero or

28
00:02:34,540 --> 00:02:41,240
more options followed by a said script and optionally followed by one or more input files.

29
00:02:41,380 --> 00:02:47,830
So let's exit out of this man page by typing Q and get to our first said command.

30
00:02:47,830 --> 00:02:50,350
Let's create a file to work with real quick.

31
00:02:50,350 --> 00:02:51,160
Let's do this

32
00:03:02,660 --> 00:03:05,140
make sure our contents made it in the file I'm sure did.

33
00:03:05,150 --> 00:03:08,000
But let's just prove it here by carrying that file.

34
00:03:08,000 --> 00:03:13,790
So sure enough it says Dwight is the assistant regional manager and that's the text that's stored in

35
00:03:13,790 --> 00:03:16,330
manager Dot T X T.

36
00:03:16,370 --> 00:03:22,060
Now as I said a minute ago the most common use of said is to act as a command line version of finding

37
00:03:22,100 --> 00:03:29,270
replace let's say you want to replace the text of assistant with the text of assistant to the to do

38
00:03:29,270 --> 00:03:35,240
that you would use the substitute command and said the substitute command is represented by the character

39
00:03:35,240 --> 00:03:35,820
s.

40
00:03:35,850 --> 00:03:37,660
So let me show you how that works.

41
00:03:37,770 --> 00:03:40,790
We'll start out with her said command.

42
00:03:40,850 --> 00:03:44,320
Next we'll put our asset script in single quotes.

43
00:03:44,450 --> 00:03:51,770
We'll tell said to perform a substitution by specifying the letter S after the s we use a forward slash

44
00:03:51,860 --> 00:03:57,070
followed by the text we want to substitute and then a another forward slash.

45
00:03:57,110 --> 00:04:01,090
In this case that text is assistant.

46
00:04:01,220 --> 00:04:07,190
By the way the forward slash is acting as a delimiter and the text between the forward slashes is called

47
00:04:07,190 --> 00:04:08,690
the search pattern.

48
00:04:08,690 --> 00:04:14,960
This search pattern is actually a regular expression so you can make very advanced searches if you want

49
00:04:14,960 --> 00:04:15,650
to.

50
00:04:15,650 --> 00:04:20,690
In this case we're keeping things simple to demonstrate the concept of finding and replacing some text

51
00:04:20,960 --> 00:04:22,130
using said.

52
00:04:22,130 --> 00:04:26,650
Next we'll type the text that is going to replace the previously specified text.

53
00:04:26,780 --> 00:04:31,760
We want to change assistant to assistant to the so we'll type this

54
00:04:34,840 --> 00:04:38,760
next we'll close our substitution with another forward slash.

55
00:04:38,860 --> 00:04:44,950
We'll call this text the replacement string that is the end of our set script so we'll supply the closing

56
00:04:44,950 --> 00:04:46,530
single quote.

57
00:04:46,570 --> 00:04:54,880
Finally we'll tell said what file to use as input and this example that file is Manager dot t t when

58
00:04:54,880 --> 00:05:01,100
we execute the command you can see that the text of assistant has been replaced with Assistant to the

59
00:05:01,240 --> 00:05:07,120
original text is Dwight is the assistant regional manager the text displayed after being transformed

60
00:05:07,120 --> 00:05:11,920
by our set command is Dwight is the assistant to the regional manager.

61
00:05:11,950 --> 00:05:19,030
Notice that I used the word displayed I used it very intentionally here in this example said is not

62
00:05:19,150 --> 00:05:25,330
altering the contents of the file it's just sending its output via standard out which is our terminal

63
00:05:25,600 --> 00:05:33,820
we can prove that the original file is unaltered by looking at its contents said can alter the contents

64
00:05:33,820 --> 00:05:39,250
of the specified file and we'll get to that shortly but first let's look at another example of using

65
00:05:39,250 --> 00:05:41,840
said to find and replace text.

66
00:05:41,890 --> 00:05:43,870
Let's use this text to work with

67
00:05:53,830 --> 00:05:57,040
let's change the text of my wife to said

68
00:06:03,270 --> 00:06:07,120
in this example we didn't reuse any of the text that we searched for.

69
00:06:07,140 --> 00:06:11,250
Instead we completely replaced it with brand new text.

70
00:06:11,250 --> 00:06:15,200
It should probably go without saying that said is case sensitive by default.

71
00:06:15,510 --> 00:06:16,950
So here's an example of that

72
00:06:24,920 --> 00:06:29,120
notice that the lower case text on my wife was not substituted.

73
00:06:29,120 --> 00:06:31,920
That's because it wasn't an exact match.

74
00:06:31,970 --> 00:06:38,240
If nothing matches said doesn't perform any alterations and therefore the text is returned without alteration.

75
00:06:38,270 --> 00:06:42,850
If you want to perform a case insensitive search you'll need to supply a flag.

76
00:06:42,860 --> 00:06:46,670
So really the format of the said s command is this

77
00:06:49,660 --> 00:06:52,960
you have s a forward slash and then a search pattern.

78
00:06:53,020 --> 00:06:58,010
Another forward slash to terminate the search pattern in the replacement string.

79
00:06:58,150 --> 00:07:02,170
Another forward slash and then any flags you want to use.

80
00:07:02,920 --> 00:07:07,240
So I'm just going to type control C here because I don't want to execute that command I just want to

81
00:07:07,270 --> 00:07:09,940
quit that command line there without running it.

82
00:07:10,000 --> 00:07:23,130
So we're going to supply the ie flag to the command which you can think of as standing for insensitive.

83
00:07:23,290 --> 00:07:29,710
Now the text gets replaced because you told said to ignore case by the way and it strikes me as a little

84
00:07:29,710 --> 00:07:30,270
funny.

85
00:07:30,280 --> 00:07:35,810
You can also use a capital I as the flag to do this in sensitive case matching.

86
00:07:35,830 --> 00:07:37,930
So this command works as well

87
00:07:43,310 --> 00:07:47,780
let's add some more lines to our loved text file and see how said handles that

88
00:07:57,230 --> 00:08:03,050
notice that I've used to greater than signs and that causes texts to be appended to the file if I just

89
00:08:03,050 --> 00:08:03,800
use one.

90
00:08:03,830 --> 00:08:07,660
It would have truncated the file but I was careful here and used too.

91
00:08:07,670 --> 00:08:13,140
So we can look at our contents here and sure enough there are two lines in that file.

92
00:08:13,250 --> 00:08:15,400
So let's go ahead and add a third line.

93
00:08:28,960 --> 00:08:29,330
Okay.

94
00:08:29,340 --> 00:08:29,830
There it is.

95
00:08:29,830 --> 00:08:32,860
Love dot text with three lines.

96
00:08:32,890 --> 00:08:35,290
Now let's try the replacement command again.

97
00:08:43,300 --> 00:08:48,970
What happens is that said reads one line from the file and executes the set commands in quotes against

98
00:08:48,970 --> 00:08:50,170
that line.

99
00:08:50,170 --> 00:08:55,360
Next it moves on to the next line and does the same thing until it reaches the end of the file.

100
00:08:55,420 --> 00:09:01,480
You'll see that my wife was replaced by the words said on lines 1 and 3.

101
00:09:01,480 --> 00:09:08,080
Line 2 was unchanged because our search pattern of my wife was not found on that line.

102
00:09:08,080 --> 00:09:35,390
Let's add yet another line to this file to demonstrate another important concept.

103
00:09:35,390 --> 00:09:37,550
Now let's run the replacement command again

104
00:09:43,850 --> 00:09:48,680
notice that on the last line only the first occurrence of my wife was replaced.

105
00:09:48,680 --> 00:09:51,840
It says I love said and my wife loves me.

106
00:09:51,860 --> 00:09:58,130
Also my wife loves the cat by default said just replaces the first occurrence of the search pattern

107
00:09:58,130 --> 00:10:00,770
on a line to override this behavior.

108
00:10:00,800 --> 00:10:02,820
We'll need to use the G flag.

109
00:10:02,900 --> 00:10:06,740
You can think of G as standing for global as in a global.

110
00:10:06,770 --> 00:10:07,460
Replace

111
00:10:16,630 --> 00:10:20,680
now the last line says that I love said and said loves me.

112
00:10:20,710 --> 00:10:23,090
Also said loves the cat.

113
00:10:23,230 --> 00:10:28,780
If you want to replace the second occurrence of the search pattern use the number two as a flag.

114
00:10:28,870 --> 00:10:31,390
If you want to replace the third occurrence use three.

115
00:10:31,390 --> 00:10:34,910
The fourth use for etc. etc..

116
00:10:34,930 --> 00:10:36,160
So let's do this

117
00:10:44,810 --> 00:10:45,490
this time.

118
00:10:45,500 --> 00:10:51,380
None of the first occurrences of the text my wife were replaced but only the second occurrences were

119
00:10:52,010 --> 00:10:55,630
on the line where only one occurrence of my wife existed.

120
00:10:55,640 --> 00:11:01,760
That line was unaltered because there was no second occurrence on the last line where my wife appears

121
00:11:01,760 --> 00:11:06,470
three times only the second one was changed by the way.

122
00:11:06,470 --> 00:11:11,370
When I'm saying things like change I don't mean that the original file has changed again.

123
00:11:11,510 --> 00:11:18,670
The contents of the file remain the same sometimes you don't want to just see the output displayed to

124
00:11:18,670 --> 00:11:22,270
your terminal but instead you want to save that output.

125
00:11:22,270 --> 00:11:26,750
One way to do that is to redirect the output of the set command to a file.

126
00:11:26,770 --> 00:11:29,290
Of course this isn't anything unique was said.

127
00:11:29,290 --> 00:11:32,140
It's just how Linux works with any command.

128
00:11:32,140 --> 00:11:35,890
So let's create a new file called my new love.

129
00:11:35,890 --> 00:11:37,050
Don t x t

130
00:11:51,790 --> 00:11:56,790
again the original file is left unaltered.

131
00:11:56,840 --> 00:12:01,960
So there you have it the original file with the original text and the new file with a redirected and

132
00:12:02,030 --> 00:12:03,530
altered text.

133
00:12:03,650 --> 00:12:09,470
If you want said to alter the file use the Dash I option to said which you can think of as in place

134
00:12:09,620 --> 00:12:15,230
editing if you want said to make a backup copy of the file before it alters it supply the same text

135
00:12:15,260 --> 00:12:32,980
right after the dash eye and said we'll append that text to the copy of the file.

136
00:12:33,080 --> 00:12:40,760
So now we have our original file named Love dot t x Tepe and another one named Love dot t t dot B A

137
00:12:40,760 --> 00:12:47,910
K said has performed an in-place edit of the original file so let's look at that now.

138
00:12:47,920 --> 00:12:54,670
So there you can see where said has replaced the text of my wife where appropriate given the command

139
00:12:54,700 --> 00:12:55,690
we ran.

140
00:12:55,690 --> 00:13:02,690
If we look at the backup file we'll see that it has the original text in it it's important that you

141
00:13:02,690 --> 00:13:07,250
don't use a space after the dash I option if you do you'll get an error.

142
00:13:07,250 --> 00:13:09,110
So let me demonstrate that here.

143
00:13:09,110 --> 00:13:12,420
So this is what not to do.

144
00:13:12,440 --> 00:13:13,570
Notice I'm using a space.

145
00:13:13,580 --> 00:13:15,080
This is going to cause an error.

146
00:13:20,940 --> 00:13:23,140
So sure enough we get an error.

147
00:13:23,220 --> 00:13:25,060
That is one little gotcha.

148
00:13:25,080 --> 00:13:25,830
You can avoid.

149
00:13:25,860 --> 00:13:29,550
So just do not use this space after Dash I.

150
00:13:29,730 --> 00:13:35,730
If you only want to save the lines where matches were made you can use the w flag followed by a file

151
00:13:35,730 --> 00:13:36,020
name.

152
00:13:36,120 --> 00:13:44,040
So let's do this let's change love to like we'll do a global replace so that no matter how many times

153
00:13:44,070 --> 00:13:50,580
love appears on the line it will get replaced by like and then we'll use the w flag and a file name

154
00:13:50,580 --> 00:13:58,570
after it so we'll use like dot t x t and then the file we're going to use as input is love t x t in

155
00:13:58,570 --> 00:14:04,450
this example said displays the entire contents of the loved text file to the screen with any replacements

156
00:14:04,450 --> 00:14:11,590
it made and it created a new file named like dot t t that only contains the lines where the replacements

157
00:14:11,590 --> 00:14:12,530
were performed.

158
00:14:12,550 --> 00:14:18,420
So let's look at that like t text file now while we're talking about input and output.

159
00:14:18,520 --> 00:14:24,790
I would like to point out that said can be used in a pipeline instead of specifying a file to work on.

160
00:14:24,850 --> 00:14:39,040
The simplest example would be to cat a file and pipe that to said like this.

161
00:14:39,140 --> 00:14:49,210
That is the exact same thing as doing this.

162
00:14:49,240 --> 00:14:54,910
This is a very common pattern with Linux commands where they can be given a file to operate on or they

163
00:14:54,910 --> 00:14:58,030
can use the data sent through a pipe to operate on.

164
00:14:58,030 --> 00:15:05,320
We've seen this with other commands such as cut OK sort unique and others command a pipeline thing is

165
00:15:05,320 --> 00:15:11,110
very powerful because you string as many commands together as needed to make the data look the way you

166
00:15:11,110 --> 00:15:12,040
want it to.

167
00:15:12,040 --> 00:15:14,950
I'm going to generate some text to work on with an echo command

168
00:15:18,600 --> 00:15:24,780
let's say we want to change a forward slash home forward slash Jason to be forward slash export forward

169
00:15:24,780 --> 00:15:28,000
slash users forward slice Jason C.

170
00:15:28,080 --> 00:15:32,970
The challenging thing about this is that the strings we want to manipulate have Ford slashes in them

171
00:15:33,270 --> 00:15:39,420
and we've been using Ford slashes as a delimiter with said one way to get around this is to escape the

172
00:15:39,420 --> 00:15:40,860
Ford slashes.

173
00:15:40,860 --> 00:15:46,360
I'm going to show you a better way in just a second but let's see how we would do it with escaping first.

174
00:15:46,470 --> 00:15:50,490
Now to escape a character simply precede it with a backslash

175
00:15:55,520 --> 00:15:58,250
so we're starting our search pattern with the forward slash.

176
00:15:58,310 --> 00:16:02,010
But the next Ford slash isn't a delimiter so it needs to be escaped.

177
00:16:02,030 --> 00:16:08,520
So if we're going to use a backslash to escape this forward slash same thing again here.

178
00:16:08,530 --> 00:16:14,340
So now we end our search pattern with a forward slash and again R next pattern starts with a Ford slash

179
00:16:14,350 --> 00:16:20,260
we have to escape it here again we're escaping a Ford slash and yet again.

180
00:16:20,680 --> 00:16:23,550
And then finally we're closing out our command here.

181
00:16:23,740 --> 00:16:29,920
That feels like a lot to keep straight and it's easy to make a typing mistake in this situation.

182
00:16:29,920 --> 00:16:34,180
A nice feature was said is that you can use any character as the delimiter.

183
00:16:34,180 --> 00:16:36,880
So let's use a pound sign instead.

184
00:16:37,000 --> 00:16:43,390
So the first character that follows the S will be treated as the delimiter no matter what that character

185
00:16:43,390 --> 00:16:43,750
is.

186
00:16:43,750 --> 00:16:48,960
So let me give you an example here.

187
00:16:49,040 --> 00:16:54,350
I'm going to use a pound sign and now said we'll treat that pound sign as the delimiter.

188
00:16:54,350 --> 00:16:59,510
So now I can just go ahead and specify the text I want to use here and my search pattern which is Ford

189
00:16:59,510 --> 00:17:00,290
slash home.

190
00:17:00,290 --> 00:17:07,130
Jason close that search pattern and now the replacement string is Ford slash export Ford slash users

191
00:17:07,190 --> 00:17:14,120
Ford's less Jason C and then we'll close out the replacement text there with a pound sign and the single

192
00:17:14,120 --> 00:17:17,000
quote finishes are said script command there.

193
00:17:17,000 --> 00:17:18,100
So let me hit enter.

194
00:17:18,320 --> 00:17:20,650
And so as you can see that works as well.

195
00:17:20,810 --> 00:17:23,500
Just to prove that any character works as a delimiter.

196
00:17:23,660 --> 00:17:26,630
Let's use another character how bout a colon this time

197
00:17:37,100 --> 00:17:38,560
OK that works as well.

198
00:17:38,570 --> 00:17:44,750
So you get the idea if you need to use Ford slashes and your search pattern or replacement text choose

199
00:17:44,780 --> 00:17:48,530
a different delimiter other than the forward slash.

200
00:17:48,590 --> 00:17:48,830
All right.

201
00:17:48,850 --> 00:17:51,920
So enough with actually performing substitutions.

202
00:17:51,920 --> 00:17:55,970
Let's take a second here to talk about when you would actually want to perform them.

203
00:17:55,970 --> 00:17:59,120
One idea is to use templates or template files.

204
00:17:59,120 --> 00:18:04,850
For example if you are constantly deploying new Web sites and use the same configuration except for

205
00:18:04,850 --> 00:18:10,250
the Web site name then it would be a good idea to create a template file that contains all the standard

206
00:18:10,250 --> 00:18:13,970
configuration and a place holder for the Web site name.

207
00:18:13,970 --> 00:18:19,040
Then you could use said to simply replace all the place holders with the actual Web site name.

208
00:18:19,040 --> 00:18:20,980
When you're ready to deploy it.

209
00:18:21,020 --> 00:18:26,990
Another example of when you could use said substitution feature is when you are migrating from one server

210
00:18:26,990 --> 00:18:33,580
to another or when you are using a restore of one server to create another new server.

211
00:18:33,590 --> 00:18:38,960
In this example you would need to find and replace the old hostname with a new one for all the files

212
00:18:38,960 --> 00:18:40,720
in the Etsy directory files.

213
00:18:40,720 --> 00:18:46,910
This is Etsy hosts and maybe Etsy hostname and there could be others depending on that systems configuration.

214
00:18:46,910 --> 00:18:53,180
Yet another example would be when you are copying configuration for a given service from one host to

215
00:18:53,180 --> 00:18:53,840
another.

216
00:18:53,840 --> 00:18:57,770
You might find yourself doing this especially if you are working on clusters.

217
00:18:57,830 --> 00:19:02,650
You would just copy the configuration from one host to the new host that you are adding to the cluster.

218
00:19:02,660 --> 00:19:07,130
Typically you'll need to change the hostname in the configuration file and you could use is said to

219
00:19:07,130 --> 00:19:14,180
do this for you said would be especially helpful if that hostname appeared many times in that configuration.

220
00:19:14,630 --> 00:19:16,490
Okay so that's pretty much it.

221
00:19:16,490 --> 00:19:23,000
With the said substitution command let's say we want to remove or delete some lines with said well how

222
00:19:23,000 --> 00:19:25,460
you do that is with the D command.

223
00:19:25,670 --> 00:19:28,850
Let's review the contents of our love dot text file

224
00:19:32,040 --> 00:19:37,950
let's say we want to remove the line that says this is lying to us you have to do is come up with a

225
00:19:37,950 --> 00:19:41,630
search pattern that will match that line and not the other ones.

226
00:19:41,670 --> 00:19:47,460
So we could use a search pattern of this because the word this only appears on the line.

227
00:19:47,460 --> 00:19:50,910
We want to delete our search pattern is this.

228
00:19:51,010 --> 00:19:52,280
And the command is D

229
00:19:55,500 --> 00:19:58,530
so to be clear here the syntax is a delimiter.

230
00:19:58,650 --> 00:20:04,970
And by default we use the Ford slash as the delimiter followed by the search pattern and a closing delimiter.

231
00:20:04,980 --> 00:20:11,070
Again this is a Ford slash and finally a D which tells said to delete the line that matches the provided

232
00:20:11,070 --> 00:20:11,760
pattern.

233
00:20:11,760 --> 00:20:14,850
We can see that this worked as our desired line is gone.

234
00:20:15,000 --> 00:20:19,000
Let's say we want to remove all the lines that contain the search pattern of love.

235
00:20:19,020 --> 00:20:25,730
Here's how we would do that.

236
00:20:25,740 --> 00:20:29,580
So now all the lines that match love are gone.

237
00:20:29,580 --> 00:20:34,650
Sometimes when I'm working with a configuration file with many lines I'd like to strip out the comments

238
00:20:34,650 --> 00:20:37,530
and blank lines to compact the output.

239
00:20:37,530 --> 00:20:42,510
That way I can see just the configuration and nothing else it makes it easier to read at least it does

240
00:20:42,720 --> 00:20:43,500
for me.

241
00:20:43,500 --> 00:20:47,730
I'm going to create an example configuration file to demonstrate this concept with

242
00:21:20,170 --> 00:21:26,350
so this sample made up configuration file is just a few lines long but you can easily imagine one with

243
00:21:26,350 --> 00:21:28,460
many many more lines.

244
00:21:28,480 --> 00:21:36,600
First let's remove the comments.

245
00:21:36,790 --> 00:21:42,640
Remember that the search pattern is actually a regular expression in regular expressions of the carat

246
00:21:42,640 --> 00:21:47,740
symbol matches the beginning of a line it matches a position and not a character.

247
00:21:47,740 --> 00:21:53,500
So if we want to match all the lines that start with a pound sign we use carat pound sign.

248
00:21:53,500 --> 00:21:58,360
This makes sure we don't accidentally delete lines that have actual configuration at the beginning of

249
00:21:58,360 --> 00:22:01,680
the line but have a comment later on that same line.

250
00:22:01,690 --> 00:22:07,120
So now we have our configuration file without the comment lines but we still have the blank lines to

251
00:22:07,120 --> 00:22:10,390
delete a blank line we'll use another regular expression

252
00:22:16,490 --> 00:22:22,040
again the carat symbol matches the beginning of the line the dollar sign matches the end of the line.

253
00:22:22,130 --> 00:22:28,340
So carat dollar sign matches a line if the beginning of the line is immediately followed by the end

254
00:22:28,340 --> 00:22:33,070
of the line set another way carat dollar sign matches blank lines.

255
00:22:33,170 --> 00:22:35,890
So we know how to do the two things we want to do.

256
00:22:35,930 --> 00:22:41,990
But now we need to do them at the same time we need to combine them to use multiple said commands or

257
00:22:42,020 --> 00:22:45,260
said expressions we can separate them with semicolons

258
00:22:53,030 --> 00:22:58,280
for each line said we'll perform the first command which deletes lines that start with a pound sign

259
00:22:58,580 --> 00:23:04,040
and then the second command which deletes blank lines to show you that you can combine different types

260
00:23:04,040 --> 00:23:06,800
of commands let's perform a substitution.

261
00:23:06,800 --> 00:23:10,240
Let's change Apache to h t TPD

262
00:23:21,600 --> 00:23:26,490
so that particular commanded three things are deleted lines that started with a pound sign the deleted

263
00:23:26,490 --> 00:23:32,340
blank lines and it also changed Apache to h t TPD just to be thorough.

264
00:23:32,370 --> 00:23:36,440
I want to share with you another way to execute multiple said commands.

265
00:23:36,480 --> 00:23:41,160
Mainly I want to show you this just in case you happen to see it in a script or in someone else's work.

266
00:23:41,160 --> 00:23:43,500
That way you'll know exactly what is going on.

267
00:23:43,560 --> 00:23:49,680
So the other way to do this is to use multiple dash e options to said one for each said command to execute

268
00:24:02,660 --> 00:24:06,860
said also allows you to specify a file containing the said commands.

269
00:24:06,860 --> 00:24:08,740
So let's demonstrate this now.

270
00:24:08,840 --> 00:24:13,520
I'm going to create a file using the echo command here of course you can use an editor if you want to

271
00:24:34,200 --> 00:24:37,820
so what we've done here is put one said command per line.

272
00:24:37,890 --> 00:24:43,950
Now we'll use the dash f option and supply the path to the file that has the said commands in it so

273
00:24:43,950 --> 00:24:46,390
we can do this said dash F.

274
00:24:46,560 --> 00:24:52,200
And then the path to our file and then the path to the file that we want to use as input.

275
00:24:52,200 --> 00:24:57,590
So here again we get the exact same result before we wrap up this lesson on said.

276
00:24:57,600 --> 00:25:04,530
Let's take a look at using addresses an address determines on what lines the said command will be executed

277
00:25:04,530 --> 00:25:06,870
on if no address is given.

278
00:25:06,930 --> 00:25:14,100
The command is performed on all lines an address is specified before the said command.

279
00:25:14,100 --> 00:25:16,860
The simplest of addresses is a line number.

280
00:25:16,860 --> 00:25:24,010
Here are the contents of our sample configuration file to refresh our memories here's an example that

281
00:25:24,010 --> 00:25:26,890
will only execute against line 2 of the file

282
00:25:35,010 --> 00:25:41,850
as you can see the search pattern of Apache was replaced by H T TPD only on the second line.

283
00:25:41,890 --> 00:25:47,340
There's also a match on the last line of the file but that was left alone because it didn't match the

284
00:25:47,400 --> 00:25:49,360
address of line 2.

285
00:25:49,530 --> 00:25:52,810
By the way some people do not use spaces after the address.

286
00:25:52,830 --> 00:26:02,760
So this is the same command without that extra space.

287
00:26:02,770 --> 00:26:06,960
You can also use a regular expression as an address to match lines.

288
00:26:06,970 --> 00:26:14,080
Let's say you want to replace Apache with H TPD but only on lines that contain the word group

289
00:26:23,960 --> 00:26:27,350
as you can see Apache was changed to HDTV D.

290
00:26:27,560 --> 00:26:31,550
Only on the line that also contained the word group.

291
00:26:31,550 --> 00:26:37,090
You can also specify a range by separating to address specifications with a comma.

292
00:26:37,160 --> 00:26:43,670
For example if we wanted to change the word run to execute but only on lines 1 through 3 we would do

293
00:26:43,670 --> 00:26:44,210
this.

294
00:26:52,630 --> 00:26:58,660
There was only one occurrence of run from lines 1 through 3 and it was changed the occurrence online

295
00:26:58,660 --> 00:27:00,080
4 was not.

296
00:27:00,130 --> 00:27:03,400
So let's extend the range to include line 4

297
00:27:11,530 --> 00:27:11,910
again.

298
00:27:11,920 --> 00:27:15,310
You can use regular expressions instead of line numbers.

299
00:27:15,340 --> 00:27:18,380
Let's say we want to change run to execute.

300
00:27:18,460 --> 00:27:24,000
Starting with the line that matches pound sign user an ending at the next blank line.

301
00:27:24,010 --> 00:27:39,610
So here's how we would do that.

302
00:27:39,640 --> 00:27:44,980
So there you can see that every instance of run that was in between the line that started with LB user

303
00:27:45,220 --> 00:27:49,330
and the next blank line was exchanged with a text execute.

304
00:27:49,330 --> 00:27:54,210
Of course there was only one match so only one substitution was made but you get the point.

305
00:27:54,250 --> 00:27:57,510
You could specify a large portion of the file if you wanted to.

306
00:27:57,610 --> 00:28:02,650
One starting point with a another starting point as your address and then change everything in that

307
00:28:02,650 --> 00:28:05,530
section while leaving everything else alone.

308
00:28:05,530 --> 00:28:13,010
It's very powerful to use addresses was said in this lesson you learned the most common use case of

309
00:28:13,010 --> 00:28:16,460
said which is to perform text substitutions.

310
00:28:16,460 --> 00:28:22,130
You learned how to replace specific occurrences as well as how to replace all occurrences of the search

311
00:28:22,130 --> 00:28:23,040
pattern.

312
00:28:23,150 --> 00:28:28,300
In addition to finding and replacing texts you learned how to delete text with the D command.

313
00:28:28,400 --> 00:28:34,100
From there you learned how to save the alterations performed by said as well as how to make backups

314
00:28:34,100 --> 00:28:37,420
of the original file so that your data is safe.

315
00:28:37,430 --> 00:28:42,740
Next you learned three different ways to execute multiple said commands on the same set of data.

316
00:28:43,100 --> 00:28:47,630
Finally you learned how to use addresses to work on very specific sections of input.
