1

I am having a tough time finding and replacing a string using vi. Source String:

&DB2_PORT;/&DB2_DATA;

New String after replace:

&DB2_PORT;/&DB2_DATA;:tcp=true

I can find &DB2_DATA; and replace it with &DB2_DATA;:tcp=true, but that is not what I need to do.

Can someone please help me understand the appropriate ':s' command in order to achieve this replacement?

1
  • 2
    If that's not what you need to do, could you add some more sample data, to make it clearer what it is you're asking?
    – Herb
    Oct 26, 2017 at 21:43

2 Answers 2

2

This should do what you want:

:%s/&DB2_PORT;\/&DB2_DATA;/\0:tcp=true/
2
  • Thanks a ton Karl !! It did help, work and save my time too. I always wanted to learn all about this. Is there a good online/book documented you can suggest to gain more knowledge of it ?
    – Maneesh
    Oct 28, 2017 at 5:35
  • The Vim documentation is good. Start with ":h :s". Oct 29, 2017 at 7:04
3

As you state, your string contains / so presumably that is tripping you up since it must be escaped if you use / to delimit the sections of your substitution command. See the other answer here for solution with escaping. But we don't have to use / as our separator. We can use any non-alphanumeric, single-byte character except \, " or |. For example, with #...

:%s#&DB2_PORT;/&DB2_DATA;#&:tcp=true#

Note: In a substitution & is replaced with whatever string was matched by your pattern.

3
  • Thank you Layer !! This worked too.. Kindly suggest me some good online content I can go through about it.. Regards, Maneesh Sharma
    – Maneesh
    Oct 28, 2017 at 5:53
  • @Maneesh, if you're looking for help in general look no further than Vim itself. It has great help files and also has a tutor which, if things are installed properly, you can run from command line with vimtutor. If you want more after the tutorial enter :help while in vim and start working your way through the "BASIC" section and then the user manual.
    – B Layer
    Oct 28, 2017 at 6:42
  • 1
    If you just want help with things like substitution than do :help usr_10.txt and you'll read about changing text with substitution, macros, ranges, the awesome 'global' command, and more.
    – B Layer
    Oct 28, 2017 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.