1834253 Members
2196 Online
110066 Solutions
New Discussion

vi help

 
SOLVED
Go to solution
William Pribble
Frequent Advisor

vi help

I need help editing a file. I know how to substitute words and characters globally using g/cat/s//dog/g but what if instead of substuting cat for dog, I want to substitute a / for ? character? Any help will be greatly appreciated. Thanks..
8 REPLIES 8
Patrick Wallek
Honored Contributor
Solution

Re: vi help

To substitute a / for a ? globally in vi do:

:1,$s/\?/\//g

The \ characters are 'escaping' the next character so that it is interpreted literally as a regular character and not as a wildcard character.
Darrell Allen
Honored Contributor

Re: vi help

Escape the "/" with "|" ie:
1,$s/?/\//g

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Darrell Allen
Honored Contributor

Re: vi help

Sorry, I made a typo. My corrected post should be...

Escape the "/" with "\" ie:
1,$s/?/\//g

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
S.K. Chan
Honored Contributor

Re: vi help

Or you can do this command line way (with out using vi)..

# sed 's/\//\?/g' test > new-test
Justo Exposito
Esteemed Contributor

Re: vi help

Hi Elaine,

The only one thing that you must know is the escape caracter is "\" then if you want to protect a caracter like "?" you will need to put a "\" before, like this:

1,$s/\?/\\/g

Regards,

Justo.
Help is a Beatiful word
William Pribble
Frequent Advisor

Re: vi help

Thanks for all the responses!
someone_4
Honored Contributor

Re: vi help

Hello
I see you already got your answer there. But here is a vi tip that I got on the forum that I love to share with you. In vi to instert the output from commands you can do

:.!ls -l

(make sure you are on a clean line, or it get's whacked).
You can insert the output from commands into your file or script.

Richard
John Carr_2
Honored Contributor

Re: vi help

Hi

everyone seems to over complecate this instead of using a / delimeter use something else such as !

ie

1,$s!dog!cat!g

John.