vi editor substitution


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers vi editor substitution
# 1  
Old 06-15-2009
vi editor substitution

Hi everyone,

I am new to linux and practicing using vim editor.
I am trying to substitute a word hi to hello

and i can do this using by the following command
s/hi/hello

but this will only substitute only the first occurrence of hi
and
s/hi/hello/g will substitute every occurrence of hi.

What if i want to only substitute the second occurrence of hi to hello?
whats the command going to be?

for example
if a file contains
hi
hi
hi
hi

then i want it to be
hi
hello
hi
hi
hi

Do I have to manually count the occurrence using /c option and say no to it? i guess there is a better way doing this .
# 2  
Old 06-15-2009
Looks like you want to do it in the second line...
then try
Code:
:2 s/hi/hello/

# 3  
Old 06-15-2009
what if my file contains

hi
asdf
z
z
hi
hi
hi

and i want it to be ( the substitution of the second occurrence not the second line)

hi
asdf
z
z
hello
hi
hi
# 4  
Old 06-15-2009
Quote:
Originally Posted by bonosungho
what if my file contains

hi
asdf
z
z
hi
hi
hi

and i want it to be ( the substitution of the second occurrence not the second line)

hi
asdf
z
z
hello
hi
hi
:1 -- Go to first line
/<pattern> -- search for the pattern, press n, and now you are at the second occurence
:. s/hi/hello/ or :. s/hi/hello/g

There is no shortcut here.
# 5  
Old 06-15-2009
You could save a bit of typing by issuing
Code:
:1
:/<pattern>/ s/hi/hello/

Or, but this might not work on all versions (tested on HP-UX)
Code:
:1
:/<pattern>/;/<pattern>/;s/<pattern>/<new>/

Problem with the second is that you'll have to type the pattern once for every occurrence you want, eg for the 2nd twice, for the 5th 5 times, ...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substitution

I am trying to do some substitutions using the substitution operator (:%s) in a text file. I want to replace all A1, A2, A3.......A100 in my text file. I used :%s/A2/SAE/g successfully until A9 but when I use A1, all the A11 to A19 is changed. How do I specify the exact match here? (8 Replies)
Discussion started by: Kanja
8 Replies

2. Shell Programming and Scripting

vi substitution

Hello, I'm trying to do a substitution in vi. which adds a field for the year to a line. If the line doesnt include a year, it should still add a field (although empty) the fields are: Country:number:number:name(and sometimes year):place this is a desired in and output: Sweden:55:32:John... (2 Replies)
Discussion started by: drareeg
2 Replies

3. Shell Programming and Scripting

substitution help

How do i substitute ' with space in a file using sed or awk i am getting the following two scenarios 1) xyz'd with xyz d if i use sed 's/xyz\\\'d/xy z/g' it is taking ' after \ as closing expr for substitution 2) xyz';d with xyz d please advice (8 Replies)
Discussion started by: mad_man12
8 Replies

4. Shell Programming and Scripting

substitution in VI editor

Hi, I have entries in a log file : /tmp/nightly_releases/releases_20090315file1.zip /tmp/nightly_releases/releases_20090315file2.zip I want a "/" to be inserted in between directory and the file to complete the path like : /tmp/nightly_releases/releases_20090315/file1.zip... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

5. Shell Programming and Scripting

Difference between "Command substitution" and "Process substitution"

Hi, What is the actual difference between these two? Why the following code works for process substitution and fails for command substitution? while IFS= read -r line; do echo $line; done < <(cat file)executes successfully and display the contents of the file But, while IFS='\n' read -r... (3 Replies)
Discussion started by: royalibrahim
3 Replies

6. Solaris

Epic Editor was not able to obtain a license for your use. Feature Epic Editor :Licen

Epic Editor was not able to obtain a license for your use. Feature Epic Editor :License server is down (1 Reply)
Discussion started by: durgaprasadr13
1 Replies

7. Shell Programming and Scripting

set EDITOR=vi -> default editor not setting for cron tab

Hi All, I am running a script , working very fine on cmd prompt. The problem is that when I open do crontab -e even after setting editor to vi by set EDITOR=vi it does not open a vi editor , rather it do as below..... ///////////////////////////////////////////////////// $ set... (6 Replies)
Discussion started by: aarora_98
6 Replies

8. UNIX for Dummies Questions & Answers

Pasting text in VI editor from a different editor

Hi, I knw its a silly question, but am a newbie to 'vi' editor. I'm forced to use this, hence kindly help me with this question. How can i paste a chunk 'copied from' a different editor(gedit) in 'vi editor'? As i see, p & P options does work only within 'vi'. (10 Replies)
Discussion started by: harishmitty
10 Replies

9. UNIX for Dummies Questions & Answers

Need help in substitution!!!!

In a file I want to globally change a "|" charater by a new line character. I am using the command 1,$s/\|/??/g Can anybody say what should I put in place of ?? in the above command? (3 Replies)
Discussion started by: uLearner
3 Replies

10. Shell Programming and Scripting

substitution

I am trying to substituted a variable to a file using sed. However, the value of that variable is not being substituted. Here is an example of my code. lf=' ' v_whole="${1} ${2} ${3} $lf" cp ${IPPDIR}/ctl/fax_sub_text_a.${4}.${5}.txt... (1 Reply)
Discussion started by: supercbw
1 Replies
Login or Register to Ask a Question