1833748 Members
2860 Online
110063 Solutions
New Discussion

Re: VI Replacing Words

 
SOLVED
Go to solution
Steven Chen_1
Super Advisor

VI Replacing Words

Gurus,

Could anyone know how to replace numerous lines of words using vi as follows:


/disk3/test_bk --> /disk7/test_bk

have to use the whole words, as /disk3 has been used in other places.


I have tried :s/disk3/test_bk/disk7/test_bk, as there are more that one '/', it doesn't work.

Thanks.
Steve
11 REPLIES 11
Alan Meyer_4
Respected Contributor
Solution

Re: VI Replacing Words

you can use a different delimiter than / in your substitute line ie...

s$/disk3/test_bk$/disk7/test_bk
" I may not be certified, but I am certifiable... "
Jean-Luc Oudart
Honored Contributor

Re: VI Replacing Words

1,$s/\/disk3\/test_bk/\/disk7\/test_bk/

=> protect slash with backslash.

Regards
Jean-Luc
fiat lux
Patrick Wallek
Honored Contributor

Re: VI Replacing Words

You must escape the '/' characters with a '\' character. To do your substitiion:

:1,$s/\/disk3\/test_bk/\/disk7\/test_bk

Alan Meyer_4
Respected Contributor

Re: VI Replacing Words

OOps, additionally, to globally make the change use "g"

:g$/disk3/test_bk$s$$/disk7/test_bk$g

That will do the entire file all at once...

Zero Points Please
" I may not be certified, but I am certifiable... "
A. Clay Stephenson
Acclaimed Contributor

Re: VI Replacing Words

You need to use :g (global replace) and to make things easier we will use "?" as the delimiter since you have "/" embedded in your patterns.

:g?/disk3/test_bk?s??/disk7/test_bk?g

The first "g" does every line; the 2nd optional g at the end handles multiple replacements within the same line; if the 2nd is omitted only the first occurence within the line is replaced.
If it ain't broke, I can fix that.
john kingsley
Honored Contributor

Re: VI Replacing Words

:1,$ s/\/disk3\/test_bk/\/disk7\/test_bk/g

1,$ will search for the pattern from line 1 through the last line

If you leave off the g at the end only the first occurence of the the pattern in the line will be changed.

\ character tells vi not to interpret the / character -- treat it as a normal character.
Steven Chen_1
Super Advisor

Re: VI Replacing Words

Appreciate all gurus answering this question. I have a verdict for each suggestion:

A. Clay --> best one and working

Pat & Jean --> ending up with //disk7/test_bk

Alan: not working.
Steve
Steven Chen_1
Super Advisor

Re: VI Replacing Words

A. Clay:

the s??, if omit one ?, is still working.

Thanks everybody.
Steve
Alan Meyer_4
Respected Contributor

Re: VI Replacing Words

Hmmm, it's virtually the same as Clay's and when I copied and pasted it to a test file it worked... In any case, what's important is that Clay's worked.

Zero points please
" I may not be certified, but I am certifiable... "
Jean-Luc Oudart
Honored Contributor

Re: VI Replacing Words

run a test and I do not have the //
e.g.
source file
/disk3/test_bk
/disk3/test1
/disk4/test_bk
/disk3/test_bk/

target file
/disk7/test_bk
/disk3/test1
/disk4/test_bk
/disk7/test_bk/

Regards
Jean-Luc
fiat lux
A. Clay Stephenson
Acclaimed Contributor

Re: VI Replacing Words

This reference explains why I used the ?? (or //) version of the command and why your version works as well. In my case, because I like code (including the stuff inside my head) that does not branch I use the // version in all cases. The replace command with 2 search strings is extremely powerful although in most cases the 2nd pattern is null.

http://sunsite.uakom.sk/sunworldonline/swol-10-1997/swol-10-unix101.html
If it ain't broke, I can fix that.