Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google site



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-15-2009
Registered User
 

Join Date: Apr 2009
Posts: 16
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 .
Sponsored Links
  #2 (permalink)  
Old 06-15-2009
Registered User
 

Join Date: Aug 2004
Location: India
Posts: 379
Looks like you want to do it in the second line...
then try

Code:
:2 s/hi/hello/

  #3 (permalink)  
Old 06-15-2009
Registered User
 

Join Date: Apr 2009
Posts: 16
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 (permalink)  
Old 06-15-2009
Registered User
 

Join Date: Aug 2004
Location: India
Posts: 379
Quote:
Originally Posted by bonosungho View Post
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 (permalink)  
Old 06-15-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
 

Join Date: Dec 2008
Location: .at
Posts: 2,960
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, ...
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
substitution in VI editor bhaskar_m Shell Programming and Scripting 2 03-16-2009 03:58 AM
Difference between "Command substitution" and "Process substitution" royalibrahim Shell Programming and Scripting 3 12-15-2008 11:20 AM
Epic Editor was not able to obtain a license for your use. Feature Epic Editor :Licen durgaprasadr13 Solaris 1 10-13-2008 08:39 AM
set EDITOR=vi -> default editor not setting for cron tab aarora_98 Shell Programming and Scripting 6 09-13-2008 03:01 AM
Pasting text in VI editor from a different editor harishmitty UNIX for Dummies Questions & Answers 10 09-10-2008 07:22 PM



All times are GMT -4. The time now is 07:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0