I have the following file:

    proto udp
    resolv-retry infinite

    #Single line of those bellow, I've added multiple for variations of it 
    remote fi.server.com 50505
    remote us-cityone.server.com 5050
    remote uk-citytwo.server.com 50500
    remote us-citythree.server.com  56546
    remote sweden.server.com 6546546
    rest of config file..

Let me be more clear, this is a OpenVPN conf file. I want to replace only that line that starts with remote servername 5555 - server name a different one.

I've added multiple remote servername 5555 lines just for an example, because they all represent variations that I might occur.

I want to match middle column only, fi.server.com , us-newyorkcity.server.com etc and replace it using sed with different name.

Why pattern like this, doesn't work?

sed -i.bak 's/\w+(-|\.)\.?\w+\.?\.com/g' filename

I've tried this one as well..

sed -i.bak 's/\w+(-|\.)\.?\w+\.?\.\w+/g' filename

Further note, from the example us-newyorkcity.server.com only server.com part is a static one. Rest is changed.

Thanks for help!

  • Can you post the expected output example ? You said only server.com part is a static one. Rest is changed. How do you want it changed ? – Sergiy Kolodyazhnyy Feb 9 '17 at 3:24
  • Please improve the title. – phk Feb 9 '17 at 8:32
  • @phk Title is updated. – fugitive Feb 9 '17 at 13:12

Assuming a variable NEWNAME with the name you want to insert, you should be able to use the following:

sed -i.bak "s|^\(remote \)\([^ ]*\)\([ ]*[0-9]*\)|\1$NEWNAME\3|" filename

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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