1833777 Members
2168 Online
110063 Solutions
New Discussion

question on vi

 
SOLVED
Go to solution
Igor Sovin
Super Advisor

question on vi

Hi!
I need to make a script in which I must substitute some symbols, after "if else" construction.
For example I need to change all "a" symbols to "b".
Is it possible to make it using vi, so then I can start my script from command line?
#./myscript
4 REPLIES 4
Mic V.
Esteemed Contributor
Solution

Re: question on vi

Igor,

Sorry, I'm a little unclear on the question. Are you trying to run "myscript", which opens another file "myfile" and changes all "a" to "b"?

In vi:

:1,$s/a/b/g

In sed:

sed "s/a/b/g"

Perl is a nice language for string manipulation -- better IMO than shells or vi.

HTH,
Mic
What kind of a name is 'Wolverine'?
Igor Sovin
Super Advisor

Re: question on vi

Hi!

thank you!
And how to do the same thing in one file?

Here is the example of myscript:

#!/usr/bin/sh
HM=$(hostname)
then I have some commands operating with hostnames, and I need to change hostnames to $HM.
To do that I have to press ":" in command mode and then type what you said. But I need to that automaticly by running myscript.
Is it possible?
I think that only sed is able to do it.
Biswajit Tripathy
Honored Contributor

Re: question on vi

Igor wrote:
> To do that I have to press ":" in command
> mode and then type what you said. But I need
> to that automaticly by running myscript.
> Is it possible?
> I think that only sed is able to do it.

Yes, you need to use sed. Use following command
in your script:

cat old_file | sed "s/old_hostname/$HN/g" > new_file

The old_file is the name of the file with old
hostname and new_file is exactly same as old file
with hostname changed to whatever string $HN set
to.

- Biswajit
:-)
Nguyen Anh Tien
Honored Contributor

Re: question on vi

Read my attached file Vi Pages - Substitution Guide .
HTH
tienna
HP is simple