Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want this command to be executed : /bin/sed 's/HOSTNAME=.*/HOSTNAME= server_domain_com /g' /etc/sysconfig/network

Instead of this one : /bin/sed 's/HOSTNAME=.*/HOSTNAME= server.domain.com /g' /etc/sysconfig/network

the string "server.domain.com" is contained in a env variable and I would like to replace the dots by an underscore before replacing the appropriate line in /etc/sysconfig/network.

Thank you!

You can do this with bash substitution too:

/bin/sed -i "s/HOSTNAME=.*/HOSTNAME=${HOSTNAME//./_}/g" /etc/sysconfig/network

The // instructs it to change all occurences (like g in sed). Do not forget the -i flag to actually change the file, not just print it.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.