howto substitute word in vi command mode


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting howto substitute word in vi command mode
# 1  
Old 06-08-2010
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.
# 2  
Old 06-08-2010
Code:
:481,515 %s/January/March/

# 3  
Old 06-08-2010
It doesn't work I wanted becasue it runs in global mode. It changes this pattern globally starting from line 481.
# 4  
Old 06-08-2010
With my vi Version 1.79, I even get a "Illegal address combination." error Smilie
# 5  
Old 06-08-2010
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

Code:
$ 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
Code:
/January

2. When you find the one you want, by pressing n to get to it, type
Code:
cw

to replace with whatever you want.

3. Go to step 2.

Last edited by Scott; 06-08-2010 at 02:24 PM..
# 6  
Old 06-08-2010
The following works with vim, you may want to try it with vi -

Code:
:481,515 s/January/March/

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

Code:
:481,515 s/January/March/g

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 7  
Old 06-08-2010
Quote:
Originally Posted by durden_tyler
The following works with vim, you may want to try it with vi -

Code:
:481,515 s/January/March/

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

Code:
:481,515 s/January/March/g

tyler_durden
Ha ha. It was the %. I feel so stoopid Smilie

(works (in VI) in Solaris too - that's good enough for me!!)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substitute grep command at run time

HI I am trying to use the following code in the shell script (using grep) usage() { echo "Usage: ./$0 <file name> <interval> <pattern>" } METRICS_FILE=$1 INTERVAL=$2 PATTERN="$3" .. if then PATTERN="grep Gx" fi COUNT=`cat ${METRICS_FILE} | "${PATTERN}" |egrep... (8 Replies)
Discussion started by: asifansari
8 Replies

2. Shell Programming and Scripting

How to substitute a word in multiple file?

Team, I want to change below parameter in all the files in a directory, Check for HOSTNAME=`hostname` Change to HOSTNAME=localhost And I tried below but, its not working ☹ find /tmp -type f -exec sed 's/"HOSTNAME\=\`hostname\`"/"HOSTNAME\=localhost/g'" Help me if I am missing... (6 Replies)
Discussion started by: natraj005
6 Replies

3. Shell Programming and Scripting

In Vi "sed" substitute word on a specific line

i need to substitute word on a specific line. I was able to do it on command line like below but it is not working in vi. command line like below: sed -e '8s/table_name/schema.table_name/' file_name. in vi table_name and schema are my positional parameters that i pass into the script. ... (5 Replies)
Discussion started by: pimmit22043
5 Replies

4. Solaris

Howto solve this disk error in Solaris in single user mode

Hi all, OS is Solaros 10 Sparc While doing Netbackup upgradation to 7.5 , the server was asked to reboot. But then it came up in single user mode, and after I typed format command it showed some disk error. bash-3.00# format Searching for disks...WARNING:... (2 Replies)
Discussion started by: manalisharmabe
2 Replies

5. Shell Programming and Scripting

sed substitute command -- need help

I am trying to do what I thought should be a simple substitution, but I can't get it to work. File: Desire output: I thought I'd start with a sed command to remove the part of the header line preceding the string "comp", then go on to remove the suffix of the target string (e.g. ":3-509(-)"),... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

6. Shell Programming and Scripting

Howto use grep command

Hi all , i am having a table which contains start date and end date for ex .. startdate enddate 12/03/2011 12/04/2012 11/03/2011 20/05/2011 11/04/2011 28/07/2011 how to grep startdate = 12/03/2011 enddate = 28/07/2011 i need output :- startdate:12/03/2012... (4 Replies)
Discussion started by: Venkatesh1
4 Replies

7. Shell Programming and Scripting

Howto get only filename from find command in Linux?

Hi every body! I would like to get only filename in the result of find command in Linux but I don't know howto. Tks so much for your helps. (5 Replies)
Discussion started by: nguyendu0102
5 Replies

8. Shell Programming and Scripting

Can "sed" substitute word on a specific line?

Hello experts, I know line number of the word I want to replace. Can "sed" substitute word on a specific line? As well, can sed substitute words inside a specific patten. ex. <word>lalala</word> #replace anything between <word> and </word> minifish (2 Replies)
Discussion started by: minifish
2 Replies

9. Shell Programming and Scripting

how to substitute more than one word in a text file?

well i have this file here: <XML> <pregate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <system_c>HPREGATE</system_c> <trans_c>HSPG</trans_c> <trans_dt>20060105161333</trans_dt> <user_id_m></user_id_m> <func_c>C</func_c> </pregate> </XML> i want to... (2 Replies)
Discussion started by: forevercalz
2 Replies

10. UNIX for Dummies Questions & Answers

Substitute Command in vi

How do I substitute a word throughout a file? For example change all instances of the word John to Mark. This would be in vi for korn shell. (10 Replies)
Discussion started by: lesstjm
10 Replies
Login or Register to Ask a Question