cancel
Showing results for 
Search instead for 
Did you mean: 
  • 1,351 Views

vi susbtitution command issue

Jump to solution

RHEL 8

vi version 8.0.1763

 

When running %s/<old text>/<new text> command in vi editor, its replacing all occurrences of <old text>. Ideally this should be replacing only first occurrence it finds and not all.

How to correct this behaviour?

Eg:

cat testfile

=======

hi

this is my first line.

this is my second line.

this is my third line.

========

In vi when I issue, :%s/hi/hello it is replacing all occurrences of "hi" where ideally it should have replaced only first occurrence.

Another thing, it is replacing only first occurrence in line. For instance, Second "hi" in "this is my third line." is not getting replaced by this command.

All help appreciated.

Thanks

Girish

0 Kudos
1 Solution

Accepted Solutions
EmanuelHaine
Flight Engineer
Flight Engineer
  • 1,327 Views

Hello Girish_Rohini,

The "%s" means that the changes will be performed on the whole file. If you want to change a specific occurrence, you should perform the command with the line number. For example, if I want to change the word "home" that is on the second line for the word "HOUSE", I perform ":2s/house/HOME/g".

Maybe should be other ways, but for now, that is all I know about that.

I hope it could be helpful.

Regards.

Emanuel Haine

View solution in original post

0 Kudos
1 Reply
EmanuelHaine
Flight Engineer
Flight Engineer
  • 1,328 Views

Hello Girish_Rohini,

The "%s" means that the changes will be performed on the whole file. If you want to change a specific occurrence, you should perform the command with the line number. For example, if I want to change the word "home" that is on the second line for the word "HOUSE", I perform ":2s/house/HOME/g".

Maybe should be other ways, but for now, that is all I know about that.

I hope it could be helpful.

Regards.

Emanuel Haine

0 Kudos
Join the discussion
You must log in to join this conversation.