cancel
Showing results for 
Search instead for 
Did you mean: 

search and replace

SOLVED
Go to solution
sekar sundaram
Honored Contributor

search and replace

Hi,
i have seen many search and replace posts.. but this issue i am not seeing..

1. in vi editor how to control the search and replace only in first 50 lines or from curser the next 50 lines or between line numbers 20 to 70?
2. i am having a string
OBJECT "<$1>"
OBJECT "NT"
OBJECT "NT_OS"
like these...
i would like to search for OBJECT "NT* and replace them with OBJECT "abc$1"
or OBJECT "N<> ---i want to search for string starting with N and it should have only x number of characters...so that i can control, whether NT or NT_OS will get replaced.

thanks...
5 REPLIES
Kapil Jha
Honored Contributor
Solution

Re: search and replace

to replce something from line 20 to 70

do

:20,70s/abc/pqr

it says replace all occurrence "abc" with "pqr"
from line 20 to line 70.

by the way you can see lines in vi with
:set nu

BR,
Kapil+
I am in this small bowl, I wane see the real world......
Dennis Handly
Acclaimed Contributor

Re: search and replace

>so that I can control, whether NT or NT_OS will get replaced.

If you only have two choices, you can just limit it with \< and or \>
:s/NT\>/abc$1/

>Kapil: to replace something from line 20 to 70

If you don't want line numbers, you can change from one mark to another:
:'a,'bs/abc/pqr/
sekar sundaram
Honored Contributor

Re: search and replace

thanks Kapil, Dennis...

one more question:
how to search for a string and replace its whole line(with confirmation)?
i mean,
:%s/OBJECT*/OBJECT "NT"/gc

OBJECT* -- i wanted to search for the line OBJECT "_ _ _ _ ..." and replace it with OBJECT "NT"
muruganantham raju
Valued Contributor

Re: search and replace


1. in vi editor how to control the search and replace only in first 50 lines or from curser the next 50 lines or between line numbers 20 to 70?

:1,50s//
:20,70s//

This will replace only the first occurance of the pattern in the given line. To replace all occurances of with append the search with 'g'

:1,50s///g






muruganantham raju
Valued Contributor

Re: search and replace

To replace OBJECT "_ _ _ _ ..." with OBJECT "NT"

:%s/OBJECT.*/OBJECT "NT"/gc