1831348 Members
3022 Online
110024 Solutions
New Discussion

vi question

 
subhashni
Regular Advisor

vi question

Hello ,
Is there any easy way to replace bunch of word in file in vi mode. For example in a file i have strings as "x123" ,"y123" ,"4123" etc.
I would like to change these strings as "0123".Please help.
Thanks

unix4me
9 REPLIES 9
John Poff
Honored Contributor

Re: vi question

Hi,

Try this:

:%s/.123/0123/g


JP
Uday_S_Ankolekar
Honored Contributor

Re: vi question

You can use global replacement method in vi mode
for Ex: if you want to replace x123 as 0123 use this way

:1,$s/x123/0123/g

-USA...
Good Luck..
Pete Randall
Outstanding Contributor

Re: vi question

How about using sed:

sed 's/*123/0123/g' filename

I think that will do it.


Pete


Pete
Patrick Wallek
Honored Contributor

Re: vi question

The command to use would be:

:1,$s/[1-9a-zA-Z]123/0123/g

The above will replace any string that starts with a number other than 0 or an upper or lower case letter followed by 123 (1123,a123,D123, etc) with 0123 and will do it for all occurrences.

A. Clay Stephenson
Acclaimed Contributor

Re: vi question

Note that "." represents any single character, so enter this vi global replace command:
:/.123/s//0123/g
If it ain't broke, I can fix that.
Pete Randall
Outstanding Contributor

Re: vi question

OK, I'll take that back. Why don't I test these things before publicly making a fool of myself? Arghh!


Pete


Pete
Steven E. Protter
Exalted Contributor

Re: vi question

I am attaching a perl script that does this work with system calls.

Makes it a bit easier.

Its pretty wasy to read code.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Caesar_3
Esteemed Contributor

Re: vi question

Hello!

Try this:

:1,$s/OLD STRING/NEW STRING/g

Caesar
subhashni
Regular Advisor

Re: vi question

Guys,
I have tried your suggetions.Really Really
I'm happy since it worked like anything.
Thanks for the useful quick reply.
unix4me