howto substitute word in vi command mode

Hi

I'm trying to substitute word "January" with word "March" in my script but in certain lines only.
I have couple words containing pattern that I want to substitute e.g.

  • log.January.1.array
  • log_January_1_array_1
  • log.January.1.array
  • log_January_1_array_11

Trying to do cmd below in command mode of vi but it doesn't work:

:%s/January/March/481-515

What should be correct syntax for this to happen ?

thx.

:481,515 %s/January/March/

It doesn't work I wanted becasue it runs in global mode. It changes this pattern globally starting from line 481.

With my vi Version 1.79, I even get a "Illegal address combination." error :confused:

The anbu23 solution is the first thing I though of, but it doesn't work for me either (OSX or RedHat) which surprised me. Especially as it works in ed

$ cat file1
a
b
c
d
c
e
f

$ ed file1 <<!
3,5s/c/X/
w
q
!
14
14

$ cat file1
a
b
X
d
X
e
f

presul, if you only have "a couple" of them, why not do it manually. You are in vi anyway!

  1. In command mode, type
/January
  1. When you find the one you want, by pressing n to get to it, type
cw

to replace with whatever you want.

  1. Go to step 2.

The following works with vim, you may want to try it with vi -

:481,515 s/January/March/

It substitutes the first occurrence of January only.
To substitute all occurrences of January in those lines -

:481,515 s/January/March/g

tyler_durden

1 Like

Ha ha. It was the %. I feel so stoopid :smiley:

(works (in VI) in Solaris too - that's good enough for me!!)

It may be due to percentage.

Yep. I've just before tried what durden_tyler suggested and it worked like a charm both in vi and vim.

Confirm. Works great.
thx again.