- In this video, we are going to explore sed.
We have just talked about awk,
and you have learned that awk is useful
for filtering text and printing specific values only
sed is a stream editor, and it allows
you to edit files in a non-visual mode.
So it's really a programmable editor,
and let's just have a look at a couple of examples with sed.
Let me pop the examples on the slide to make it easy
for you to repeat them yourself.
And let me just demonstrate.
So, to start with sed minus N five P on etc Pass WD,
which is doing what? This is printing line five, and
if you don't believe me, well, let's do a head minus five
etc Pass WD pipe till minus one.
Which is doing the exact same thing.
Line five.
Good. Now, one thing that you cannot do so easily
with other utilities from the command line is
the following, sed minus I.
We need a minus I to make sure that the
manipulation is written to the target fell and
not printed on the standard out. S which is substitute.
And I am going to use substitute Anna for Bob,
and I'm making that a global substitute on users.
And what is the result?
Well, we can see the result
in the users fell everywhere, where we had Anna before.
Now we have Bob. Notice, there is no easy way to undo that.
So make sure that you know, what you are doing.
Also convenient, is the option to use sed, to
to delete entries from a file.
It may happen that
while you are doing whatever manipulation
you get an error, on a line, in a file.
And if you identify the line number
then you can use sed minus I, where the minus I
is writing the manipulation directly to the file minus E
which is an added command.
And the added command is two D. Two is the line
number, D is delete.
And we do that on users.
And what do we get?
Well, as you can see, the second line, which had
Bob belle has been deleted, that doesn't make sense anyway
to have a Bob belle line.
Now you can also use sed, in a shell loop.
So, let me create a couple of files, echo, hello
file one TXT, and I need to do a redirect.
This is something we haven't seen before.
Echo, hello, normally prints a text.
Hello, on screen.
I don't wanna have the text.
Hello, on screen.
I want to redirect it to a file.
That's why I use the redirect, which means
that the output of the command is written to the file,
and I'm specifying right here.
And I'm just creating a couple of files, four of them.
That's pretty dumb.
But what I want to show you is that sed is
particularly useful in a small looping structure for I
in star TXT do sed minus I slash minus I S
slash hello slash bye slash G single quote dollar I Don.
So what is that?
Well, this is actually a very simple shell script,
a short iteration.
The first part of the iteration
for I in start TxT really means
for each element that matches the pattern, start dot TxT.
We are going to treat them one by one.
And how do we do that?
Well, we do that with the due part.
And in the due part, we are using a set
minus I that's a substitute operation on dollar I, dollar
I is the name of the file in the current iteration,
and don't is closing the iteration loop.
And if I would use cat on file one dot TXT there, we can see
that the substitution has been executed the right way.
This is a small shelf scripting iteration.
Shelf scripting is a powerful solution in Linux as well.
Not something that we are going to explore in discourse,
but I definitely recommend that
the next thing that you are going to learn
about Linux to become a Linux power user, is shell
scripting's, pretty important part of your Linux toolbox.