Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search_string/replacement_string (vi, sed) 2

Status
Not open for further replies.

charlesy

Programmer
Aug 3, 1999
26
AU
When using vi, or sed. I run a %s/search/replace and these two tools only seem to replace the first instance of the string.
ie. in vi
dog dog dog dog dog cat cat dog hello
then
:%s/dog/cat
will only change to
cat dog dog dog dog cat cat dog hello
is there an option to change all or another tool to use.
I'm using a Hp Unix B.11.00 U 9000/800


Contrary to popular belief, Unix is user friendly.It just happens to be very selective about who it decides to make friends
with.


 

When I use vi this is what I do.

:1,$ s/pattern/replace/g

The "g" stands for global search and replace. If I omit the "g" option it will only replace the first instance.

I think sed follows this same rule.

-Danny






 

Hi, charlesy and abovebrd!

My vile (vi clone for DOS and Win) replace also only the first instance of pattern with command s/pattern/replace.

1,$ are same as %.

I also use option c - confirm global substitutions: %s/pattern/replace/gc.

Bye!

KP.
 
you could always try the ':g/<search string>/s//<replace string>/g'

which means globally replace the string (globally per line) in the document
 
Jad,

The synax you are using, what is it ? I know its general expression. But in what contexts would I use it. It this a sed command ?

Would it work within VI ?

Just wondering, as I have not seen that before.

-Danny






 
Jad's example comes from either the ed or ex editors, both of which are part of vi (I forget which)

:g/some_string//new_string/g

and

:%s/some_string/new_string/g

do the same trick

some trivia...

notice the :g command

:g/regular expression/(and then some command)

the command

:g/Hello World/p

prints all of the lines containing the string &quot;Hello World&quot;, just like the grep program does..

as in...

:g/re/p

I'm a mine of such rubbish.... Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top