[No audio]
Hello everyone and welcome again to
our newer lesson sed command.
sed command is a substitute command which
replaces or substitute a text with another
text or manipulate your file.
It is a very very powerful command.
So what are the few things that sed command does?
It replaces a string in a file with another string.
So a string is a word, a sentence, text
message or any kind of text message inside of
your file that you want to replace.
It finds and deletes a line.
So if a line has ABC and you want to get rid
of that line in that text file, well you could go in
and use the sed command and delete that line that contains ABC.
Now wait a second Imran hey, why can't I do the same thing
when I go to vi and find the line and delete it?
Yes, you could definitely do that.
But what if you have a file that has let's
say a million lines, and in those million lines you
probably have at least 1000 lines that has ABC.
Now are you going to go into the file
and delete or replace that string one by one?
You think that is efficient? Not at all.
So that's why sed command is so powerful.
That allows us to manipulate the text inside a file.
Another example that we'll learn is how
to remove empty lines from our file.
If there are more than one empty lines,
how can you get rid of it?
Yes, of course, going back to the same thing that you
could go into the vi and dd and remove it.
But again, this is not productive.
Remove the first line or number of lines
in a file, to replace tabs with spaces.
These are the very few examples of sed commands that
I'm going to go over and substitute within vi editor.
So if you are already in vi, and you
wanted to replace a text xyz or text or
anything with another text or another word, you could
also do by going straight into the vi.
But again, am I negating my
statement, what I said before?
No, because when you are going to the vi
and you look for one word and then you
replace it, again it's a manual process.
But there is another way in vi that
you could just find that text one time
and it replaces it everywhere in one shot.
And if there is so much more and more, that sed command
has to offer. So I'm going to log into my Linux machine,
I'm going to log in as a PuTTY client and I will
show you a few examples of how sed command works.
I'm logged in as myself.
I am in my home directory.
I have a file in my home directory,
that is called, let's do ls -ltr here,
that is called seinfeld-characters.
So if you do cat on seinfeld-characters,
you'll see all these characters of Seinfeld.
Again, it's just a list of all
the characters that played in Seinfeld.
If you do not have that file or if you
did not create that file previously, then go ahead and
you could just pause it right here and create that
file with all those names that I have.
Or if you want to use a
different file, different names, that's fine.
It's just the example that I'm going to
be using to explain how sed command works.
So I already have this file.
Now, the first thing I wanted to do,
let's say, what if I wanted to replace
one of the word with a different word?
Let's say I wanted to replace Kenny.
There's a character named Kenny Bania.
How about if I wanted to replace
his name from Kenny to Lenny?
You would use the command sed, single quote s for substitute,
Kenny with Lenny, so slash Lenny,
and slash again,
and if there are more than one
Kenny, then you could type g,
because this g stands for global.
It will replace the entire file
wherever you have the word Kenny.
So by default, I always use g.
You should also get into the habit of
g if you want to replace it everywhere.
So you close the single quote, and then
you type the name of the file where
you are trying to replace the word Kenny.
Hit enter.
And now you will see, at the
bottom, it is replaced with Lenny.
Now, another thing I do want to mention that this
is going to display the change only on the screen.
It is not actually going to make a change in the file.
So if you cat out the file, you
will see the name Kenny still there.
But didn't we just replace it with Lenny?
Yes, we did, but it did not
make the change inside the file.
It only makes the change to the screen.
And if you want to have that output to
another file, you could do that and that change
will be reflected in the newer file.
But if you want to make a change to the existing
file, then what you have to do is, you come back
to the same command by hitting up arrow key and you
type -i, that is to insert the changes into the
file, the same command, and you hit enter.
Now, when you do cat seinfeld-characters, you'll
see the name has changed to Lenny permanently.
So this is how you replace Kenny
with Lenny and save the file.
Now, let's see,
what if you want to replace the word Costanza.
[No audio]
So it works the same way.
So you do sed
[No audio]
substitute Costanza,
[No audio]
and this time you just don't want to replace anything.
You just want to remove that word.
So you just put Costanza and
don't put anything in there,
g, and the name of the file characters.
You see anyone who had the
last name Costanza, that is gone. See right here.
Frank Costanza is gone.
Stell Castanza is gone.
Another Costanza I have, the bottom is gone.
George Costanza is gone.
But again, this is gone only on the screen.
And if you want to make a changes to the
file, you have to use the -i option.
I don't want to use the -i option this time
because I wanted to show you how the sed works.
So this way the file won't change.
Let's just confirm our file has all the correct
names that I want to have them in it.
So it does have it.
Next one I wanted to show you is how
to find a string of line and remove it.
If you see, this is the second
one in our slide, find and delete.
So what if I wanted to delete
every line that has the word Seinfeld?
So the command to do that is sed
[No author]
Seinfeld,
and this time type d.
[No audio]
d is to delete any character
or any word that comes up, that matches Seinfeld,
that line will be deleted.
So I am not using -i option because it will
actually make the changes to my file and save it.
I just want to show you.
So I'll just run it and you will see
all the lines that had the Seinfeld are gone.
See, none of this has the word Seinfeld in it.
So that's how you replace or delete a
line that matches a keyword or a word.
The next thing I wanted to show you is
how you could remove empty lines from a file.
So if I have empty lines, let's make the empty line.
I'll go into seinfeld-characters and
I'm going to make empty lines.
[No audio]
So I have four empty lines, right?
I'll just make one empty line right here as well, five empty lines.
Now I'm going to save that file,
cat seinfeld-characters,
[No audio]]
and you will see right here I have, five empty lines.
[No audio]
five empty lines.
I wanted to get rid of these five empty lines.
The command to do that is sed,
[No audio]
slash,
[No audio]
cap, cap shows
that anything that starts, which is right above number
six and dollar, anything that ends with dollar meaning
nothing, then just delete that, sorry, seinfeld-characters.
So anything that is starting or anything that
is ending has nothing in the middle, which
is the empty file of course, right.
So whatever that finds, please go ahead and delete it.
So hit enter and you'll see everything is gone.
But now if you do seinfeld-character again,
you'll see it will still have the line. Why?
Because we did not specify the -i option and I
want you to go ahead and specify -i option.
Do a seinfeld-character, and you'll see
all the empty lines are gone.
Now what if I wanted to remove
the first line in a file?
I just want to remove Jerry Seinfeld, which
is the first line of the file.
So I will do sed, quote, 1d, quote close, the name of
the file, and you'll see the first line was Jerry Seinfeld.
That is gone. Now
of course, again, I keep saying it is not going to
affect our file because we did not use -i option.
Now, what if you want to remove first two lines?
Well, hit up arrow key and just type one comma
two and it will delete the first two lines.
That's how you delete two, three, or four, or more lines.
Now, what if you have tabs in your file
and you want to remove those tabs with spaces?
So if I do vi on seinfeld-characters and instead of
spaces, I'll just put tabs, tabs, and tabs, save the file.
You could do cat seinfeld-character.
You see three lines have tab.
I wanted to remove all the
tabs inside the file with space.
So the command to do that is sed substitute slash,
we have to use backslash and t.
That tells the operating system or the
shell that this is a tab.
Anywhere you find tab, please remove it
with space and apply it globally.
seinfeld-characters, you see,
oh, didn't like something,
[No audio]
one second.
[No audio]
Oh, my mistake.
I used backslash here.
So I have to use forward slashes here, because this slash
right here and this slash right here in the middle right
here is the string that we are trying to replace.
We are trying to replace that string with what,
with empty line.
So now that should work.
And there it is.
It did work, but it is again
not going to impact our file.
I'm going to use that command with -i
option so it will make the change to my
file and bring it back to this original content.
[No audio]
Okay, so now that's how you
actually replace tabs with spaces.
If you want to do the same thing, replace spaces with tabs,
you could do the same thing, but just
switch up backslash t with the second space holder.
So you move this thing to here.
So it will remove everywhere, let's say,
even if you want to try it,
t, and here I have,
so any spaces that's in
there, please replace it with tab.
And I have to put this here.
[No audio]
Okay. So I believe
[No audio]
seinfeld-characters, and there you have all the tabs in there.
I did not want to make the changes.
That's why when I was checking it,
it was not coming up. Anyway,
I wanted to bring it back.
To bring it back, I'll run the earlier command, which
is this replace tab with spaces and hit enter.
And now do seinfeld-characters, and you'll see
my format is back the way I want.
Okay, now, what if I wanted to show only
lines from 12 to 18 from a file.
So I just wanted to view 12 to 18.
So 1, 2,3, 4, 5, 6, 7, 8, 9, 10, 11, 12.
So I wanted to see all the lines starting with J Perterman
all the way to the end, which is the 18th line.
I don't want it to view any of these lines.
So for that, you'll use the command sed again,
with -n option, 12 from 12 to 18,
p is the option that you have to pick
and you hit enter and you will see right
here it's giving me from line 12 to 18.
Now what if you want to view the opposite way, meaning
you want to view everything except line 12 to 18.
Then you will run the command sed,
now don't use -n, use 12,18d this time
and seinfeld-characters, and you'll see you have all the
results from the top except starting line 12 to 18.
Now what if you wanted to put a space in
your file like every name, there should be a space,
there should be an empty line after each character.
So you'll do sed, just G, seinfeld-characters and
you'll see every line has a space, empty line,
then the name, empty line, then the name.
This is how you could add it.
But of course it's not going to impact our file.
You could have that changes by putting in -i option.
Okay, now what if you wanted to replace like, let's
say if I have seinfeld-character and I want to
replace Seinfeld with s or any other name, but I
do not want to replace number line 8.
So 1, 2, 3, 4, 5, 6, 7, 8, I want to replace
Seinfeld with S, except the 8th Seinfeld.
So you do sed single quote, do not
do 8, and substitute Seinfeld with s.
Do it globally,
[No audio]
seinfeld-characters.
Okay, see it did change, oh 1 second.
Let me see.
[No audio]
Now umm,
[No audio]
let's see.
[No audio]
So if I just type 8 just up to seinfeld for s,
so it is still not doing it.
Am I doing something wrong?
Yes, I am doing something wrong.
See, sometimes just tiny bit of
a mistake makes a huge difference.
If you notice there is no such word as seinfled.
I put seinfled.
Alright, it's a good exercise.
You hit enter, you see every Seinfeld text
or word has been changed with S.
But now let's go back to our original question.
I don't want to replace number eight.
I'll fix my spelling, hit enter and you will see
all of them have changed except line number eight.
Now the last one I talked about
substitute while you are in vi editor.
So go ahead and type vi, the name of
the file, and now you are in vi editor.
Now what if you want to replace
every seinfeld-characters while you are in vi.
So you will do colon
percent sign, s for substitute, slash,
Seinfeld to
[No audio]
Peter.
Right. You might replace everyone
of them and hit enter, and you'll see every
Seinfeld has been replaced with the word Peter.
That's how you make the change
while you are in vi editor.
Now I want you to go ahead and try it out.
I have also an article mentioned here in this
lesson that you could view that article and try
all the different commands. Again, sed command is very
powerful if you really know inside out.
So I would encourage you to try many different ways.
Go ahead and do your Google, go online and search
for anything that you wanted to do with sed.
And I promise you, every time you will type something,
make sure you type, like how to
replace a string in sed command.
Make sure you put in sed command.
And I'm telling you, you're going to
find tons of article out there
people, again, have done an amazing job who
have helped people, and you could look at
their article and see how things are done.
You can learn on that.
All right, if you have any questions about sed command, you
could just simply send me an email or message me.
Alright, good luck.
[No audio]