0

I have a file with numbered host names. I want to change the name while preserving the numbers.

CHANGE THIS:

server1.domain.com
server2.domain.com
server3.domain.com
server4.domain.com
server1.otherdomain.com
server2.otherdomain.com

TO THIS:

host1.domain.com
host2.domain.com
host3.domain.com
host4.domain.com
server1.otherdomain.com
server2.otherdomain.com

I have tried this but it does not work :%s/server*.domain.com/host*.domain.com/g

2
  • 2
    Neither vi nor Vim use wildcards in their :substitute commands. They use regular expressions, which are explained in :help pattern. Commented May 8 at 18:35
  • Not sure if they qualify as programming tools, but at the very least can you please pick one editor to ask about? Asking about multiple different editors is off-topic here because it's too broad a question. Commented May 13 at 17:58

1 Answer 1

1

You could use a capture group, and make the match a bit more specific by matching the numbers and escaping the dot.

Then in the replacement use the capture group denoted as \1

:%s/server\([0-9]\+\.domain\.com\)/host\1/g

Result:

host1.domain.com
host2.domain.com
host3.domain.com
host4.domain.com
server1.otherdomain.com
server2.otherdomain.com
Sign up to request clarification or add additional context in comments.

2 Comments

I'd have gone for :%s/server\ze\d\+\.domain\.com/host/ but the result is the same. I just happen to like \zs/\ze.
Who doesn't love \zs and \ze?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.