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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vi question 1

Status
Not open for further replies.

rondebbs

MIS
Dec 28, 2005
109
US
I should know this but here goes -

I have a script that I am editing with vi. Is there a simple way to change all the occurences of a word to another word? For example if the word "dog" appears 15 times, how can I change all 15 occurences to cat?
 
:s/RE/string/opt Search-and-replace; string is the replacement. Use opt to specify options c (confirm), g (globally on each line), and p (print after making change).

:s/dog/cat/g


regards,
Khalid
 
whole file, all occurrences on every line like so
:%s/dog/cat/g

whole file, all occurrences on every line with user confirmation like so
:%s/dog/cat/gc


HTH,

p5wizard
 
Same thing but I use

:1,$s/dog/cat/g

where 1 is line 1
where $ is end of file

so if you wanted just to change lines 1 thru 30 you could do

:1,30s/dog/cat/g

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
As a general rule the vi editing commands are the sed editing commands - their both derived from ed. One difference I have found is that braces need to be escaped. i.e.
Code:
:%s/ \{4,20\}//g
will delete any block of 4 - 20 spaces anywhere in the file. Note that the pattern is greedy. i.e. 22 spaces will be seen as 20 spaces followed by 2 spaces, not 4 spaces followed by 18 spaces.

Ceci n'est pas une signature
Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top