Assistance Needed With Find/Replace in Vi

Hello All

I have always had a question about find and replace in Vi. As this uses Vi, sed, and RegEx I never knew how or where to post the question but I thought I would give it a shot here. Say I have a text file filled with the following:
Sue, your IP address is 192.168.1.10 which is in the office.
Bob, your IP address is 192.168.1.20 which is in the conference room.
Dan, your IP address is 192.168.1.30 which is in the store room.
Sam, your IP address is 192.168.1.40 which is in the lobby.
Jon, your IP address is 192.168.1.50 which is in the kitchen.

Now say I want to change it to:

Sue, your IP address is 192.168.1.10 for today, which is in the office.
Bob, your IP address is 192.168.1.20 for today, which is in the conference room.
Dan, your IP address is 192.168.1.30 for today, which is in the store room.
Sam, your IP address is 192.168.1.40 for today, which is in the lobby.
Jon, your IP address is 192.168.1.50 for today, which is in the kitchen.

I am not sure how to add the " for today" bit without deleting the rest of the line.

The following RegEx is as far as I can take it. The "???" marks the spot where I need help.

:%s/address is [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/??? for today/

I have seen some use of a $1 variable but I haven't had much luck with it. Would anybody be kind enough to clue me in to the secret?

Thanks for reading and have a good day. :slight_smile:

Hello,

Perhaps it's not what you're looking for, but I would replace the "which is" part by "for today, which is":

%s/which\ is/for\ today,\ which\ is/g

HTH,
Lo�c.

Hi.

:%s/which/for today, &/

$1 is typically a positional parameter used in a script, or some programs which accept arguments. Regular expressions use a different notation, such as \1, \2, etc, and the & used above means:

HTH

I must apologise, I believe I gave a bad example. I had to come up with it pretty quick and probably didn't do a good job. What I am looking for is a way to search for part of the string that is variable but with a known pattern, for example an IP address:

[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}

and add/remove/change things at that part in the string without affecting the rest of the original string or the variable pattern. Which in this example is the IP address. Let me know if I am not making myself clear.

Thanks for reading and replying. :slight_smile:

Hi NoSalt.

Your regular expression is generally OK, just need to escape the curly brackets:

:%s/[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}/& for today,/

Gives:

Sue, your IP address is 192.168.1.10 for today, which is in the office.
Bob, your IP address is 192.168.1.20 for today, which is in the conference room.
Dan, your IP address is 192.168.1.30 for today, which is in the store room.
Sam, your IP address is 192.168.1.40 for today, which is in the lobby.
Jon, your IP address is 192.168.1.50 for today, which is in the kitchen.

Another version that works is:

:%s/[0-9\.]\+/& for today,/