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
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
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
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.