Got a problem which I’m not sure why it’s happening and wondered if you could help?

I have written the code below to search the /etc/hosts file to search and replace all entries which have the host name and domain name suffix with what the user has input when the whole script has been run.

For example I want to change the below within the /etc/hosts file

servername.domainsuffix.net servername

to

newname.domainsuffix.org newname

My problem is that where I have placed the $uname (which is the uname –r output from the system) it is not being replaced. However where I have the domain name suffix part which is an actual name it does replace this and $uname replaces the server name after the FQN if you get what I mean.

Apologies if this is not clear it sounds ok in my head :-)

From the example above I get the following Servername.domainsuffix.org newname

open(HST, "<$hosts"); @HOST = <HST>; close(HST); open(HST, ">$hosts"); foreach (@HOST) { s/$uname/$answer1/g; s/domainsuffix.net/$answer4/g; print HST $_; }

In reply to Search and replace within a file by muizelaar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.