presul
June 8, 2010, 12:41pm
1
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.
anbu23
June 8, 2010, 12:43pm
2
:481,515 %s/January/March/
presul
June 8, 2010, 12:54pm
3
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
Scott
June 8, 2010, 1:18pm
5
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!
In command mode, type
/January
When you find the one you want, by pressing n to get to it, type
cw
to replace with whatever you want.
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
Scott
June 8, 2010, 1:51pm
7
durden_tyler:
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
Ha ha. It was the %. I feel so stoopid
(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.
presul
June 13, 2010, 8:21am
10
Confirm. Works great.
thx again.