Hello everyone and welcome again to our newer lesson said Command said Kamen is a substitute command
Britcher places or substitute a text with another Tex or manipulate your file.
It is a very very powerful command.
So what are the few things that said command does.
It replaces a string in a file with another string or string is a word a sentence text message or any
kind of text message inside of a file and 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 set command and or delete that line that contains ABC.
Now wait a second Imraan.
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 thousand 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 I said command is so powerful that allows us to manipulate the text inside a file.
Another example that you'll learn is how to remove empty lines from a 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 few very few examples of set commands that I'm going to go or and substitute for the vi
editor.
So if you are already in vi and you want to place a text x y z or T x t 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 vi and you when 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 replaces it everywhere
in one shot.
And if there is so much more and more that is that said commands has to offer.
So I'm going to log into my Linux machine and log in as a putty client and I will show you a few examples
of how set 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 this to myself your dad 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 the file or if you did not create that for 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 I use a different file a different names that's fine as just the example that I'm going to be
using to explain how set command works.
So I already have this file.
Now the first thing I want 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 place Kenny there's a character named Kenny banya.
How about if I want to replace his name from Kenny to Lenny you use the command said single code as
per substitute Kenny wedde Lenny slash Lenny and slash again.
And if you if there are more than one Kenny then you could type G because this G is stands for Global.
It will replace the entire file where ever you have the word Kenny.
So by default I always use g.
You should also get into the habit of G if you want replace every everywhere.
So you close the single code 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 to change only on the screen.
It is not actually going to make a change in the file.
So if you cut out the file you will see the name Kenny still linger.
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 do you have to do is you come back to
the same command by hitting up arrow key and you type minus.
That is to insert the changes into the file.
The same command and you hit enter.
Now when you do cat Seinfeld characters you will see the name has changed to Lenny permanently.
So this is how you replace can't even Lenny and save the file.
Now let's see what if you wants to replace the word stanza so it works the same way so you do said
substitute Castanza
and this time you just don't want to replace anything you just want to remove that word.
So you just put stanza and don't put anything in there G.
And the name of the file characters you see any anyone who had the last name could stanza that is gone.
So you were here Frank kickstands is gone Estelle Castanza is gone.
And now Cassandra I have the bottom is gone.
George kickstands is gone.
But again this has gone only on the screen if you want to make a changes to the file you have to use
the minus option.
I don't want to use the minus option at this time because I want to show you how to set work so this
way won't change.
So just that's just confirm our file has all the correct names that I want to have in it.
So it does have it.
Next one I want 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 want to delete every line that has the word Seinfeld.
Look for the command to do that is said
Seinfeld.
And this time type d
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 minus either option because it will actually make the changes to my file and save
it.
I just want to show you so I just oh just run it and you will see all the lines that had the Seinfeld
are gone.
See none of us has the word Seinfeld in it.
So that's how you replace or delete a line that matches a key word or a word.
The next thing I want to show you is how you could remove empty lines from a file.
So if I have empty lines let's make them two line I'll go into Seinfeld characters.
And I'm going to make empty lines
so I have four empty lines right.
I'll just make one empty line right here four five empty lines.
Now I'm going to save a file cat Seinfeld.
Characters
and you'll see right here I have
five empty lines.
I want to get rid of these five empty lines.
The command to do that is said
stash
cap caps shows that anything starts which is right above number six and dollar anything that ends with
dollar meaning nothing then just delete that sign.
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 you'll still have the line.
Why.
Because we did not specify the minus option.
And I want you to go ahead it's best.
A-minus i option do Seinfeld characters and you'll see all the empty lines are gone.
Now what if I want to move the first line in a file.
Just one or more Jerry Seinfeld which is the first line of the file so I will do said code one D.
Close the name of the file and you'll see the first line Sergey.
Jerry Seinfeld.
That is gone now of course again they keep saying it is not going to affect our file because we didn't
use mine sigh option.
Now what if you wanted to move first two lines.
Well hit up arrow key and just type one comma too 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 and your file and you wanted 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 and you see three lines have tab.
I wanted to remove all the tabs inside the file space.
So the command to do that is said substitute slash.
You 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 didn't like something
once.
And
my mistake I used backslash here 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're trying to please.
We're trying to replace the string with what would empty line.
So now that should work.
And there it is.
It did work but it is again not going to impact our file and use that command the minus option.
So it will make the change to my file and bring it back to its original content.
OK so now that's how you actually replace tabs would word spaces if you wanted to do the same thing
replace spaces with tabs.
You could do the same thing but just switch a backslash t would with the second space hole.
So you move this thing to here so it will remove everywhere.
Let's say even if you want to try it.
And here.
So any spaces that it's in there please replace it with tab and I have to put this here
OK so I believe
sign of characters.
And there you have all the tabs.
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 around the earlier command which is this sort of place
tab spaces and hit enter and now do Seinfeld characters and you will see my format is back the way I
want.
OK.
Now what if I wanted to show only lines from 12 to 18 from a file so I just wanted to view it 12 to
18.
So one two three four five six seven eight nine 10 11 12.
So I want to see all the lines starting with J.
Peterman all the way to the end which is the 18th line.
I don't want to view any of these lines.
So for that you'll use the command said again.
But mine is an 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 wanted to view the opposite way meaning you want to do everything except line eight
to 12 to 18 then you will run the command said.
Now don't use minus and use 12 18 D this time and Seinfeld characters and you will see all you have
all the results from the top except starting line 12 to 18.
Now what if you wanted to put a put a space in your and your file like every name there should be a
space.
There should be a empty line after each character.
So you to said just gee Seinfeld characters and you see every line has a space empty line and the name
empty line and the name.
This is how you add it.
But of course it's not going to impact our file you could have that changes by putting in mind's I option.
OK.
Now what if you a to place like let's say if I have a Seinfeld character and I want to replace Seinfeld
with S or any other name but I do not want to place number line eight.
So one two three four five six seven eight.
I wanted to play Seinfeld.
S except the eighth Seinfeld.
So you do said single quote do not do a and substitute Seinfeld.
S do it globally.
Seinfeld cactus see it change.
Once again we see
no
see.
So if I just type a subtrees type of s so it is still doing it.
Am I doing something wrong.
Yes I am doing something wrong.
See sometimes just a tiny bit of a mistake makes a huge difference if you notice there is no such word
as sign fled.
I put signed fled.
It's a good exercise.
You had an ter you see every Seinfeld text award has been changed with s.
But now let's go back to our original question.
I don't want to replace number 8 fix my spelling error and you will see all of them have changed extra
except line number.
Now the last one I talked about substitute was you are in vi Ed. So go ahead and type VI.
The name of the file and now you are an editor.
Now what if you wanted to replace every single character while you are in the eye.
So you will do.
Cullen percent sign.
S for substitute slash Seinfeld to Peter right.
You want to replace every one with the and slash hit enter and you'll see every Seinfeld has been replaced
with word Peter.
That's how you make the change while you are in the editor.
Now I want you to go ahead and try it out.
I have also an article mentioned here and this lesson that you could view that article and try all the
different commands against said command is very powerful.
If you really know inside out so I would encourage you to try many different ways.
Go ahead and do do your google go online and search for anything that you wanted to do with said.
And I promise you every time you'll type something.
Make sure you type like how to replace a string in said command.
Make sure you put in set command.
I'm telling you you're going to find tons of articles out there.
People again have done 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 question about set command you could just simply send me an email or message
me.
Good luck.