Replace string in vi

Hello,
Need vi script.
file:

dog1!cat!
dog2!bird!
dog1!fish!
dog2!lion!
...

expected:

dog1!bird!
dog2!NULL!
dog1!lion!
dog2!NULL!
...

Need replace from dog2!"word"! to dog1!"word"! and write in dog2!NULL! and the same logic for all strings where are dog1 and dog2

Any idea?

thanks

In the ":" mode:

%s/dog2!.*/dog2!NULL!/

with your script result are:
dog1!cat!
dog2!NULL!
dog1!fish!
dog2!NULL!

but my expected result are from:
dog1!cat!
dog2!bird!
dog1!fish!
dog2!lion!
...
to:
dog1!bird!
dog2!NULL!
dog1!lion!
dog2!NULL!
...
do you see differences?

thanks

Try:

%s/dog1.*\ndog2!\(.*\)/dog1!\1\rdog2!NULL!/
1 Like

Results:
dog1!cat!
dog2!bird!
dog1!fish!
dog2!lion!

substitute pattern match failed

Any idea?

I guess you are using original "vi", not "vim" on which I tested this command... Does it have to be done in vi? If not, you can try something like this:

perl -p0e 's/dog1.*\ndog2(.*)/dog1\1\ndog2!NULL!/g' file
1 Like

Yes, it have to be done in vi.

thanks

In vi editor, try this..

%s,dog1!.*,NULL,g|%s,dog2,dog1,g|%s,NULL,dog2!NULL!,g