Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

According to the following Perl command

( this command part of ksh script )

I can replaced old hostnames with new hostnames in Linux or Solaris


 export previos_machine_name
 export new_machine_name


      perl -i -pe 'next if /^ *#/; s/(\b|[[:^alnum:]])$ENV{previos_machine_name}(\b|[[:^alnum:]])/$1$ENV{new_machine_name}$2/g'  file

EXPLAIN: according to perl command - we not replace hostnames on the follwoing case:

RULE: [NUMBERS]||[letter]HOSTNAME[NUMBERS]||[letter]

my question

after I used the Perl command in order to replace all old hostnames with new hostnames based on the "RULE" in the Perl command

how to verify that the old hostnames not exist in file ?

for example



   more file

   AAARed_Hat_linux1a          verification should be ignore from this line
   @Red_Hat_linux1a$             verification should be match this line
   P=Red_Hat_linux1a            verification should be match this line
   XXXRed_Hat_linux1aZZZ         verification should be ignore from this line
share|improve this question
Are we talking a few (less than 10) machines or a lot here? – John Siu Dec 10 '12 at 11:31
why its important ? , any way is could be 1 machine or more then 20 machines depend file configuration , and in my script each time I verify one hostname ! – yael Dec 10 '12 at 11:33
Well, if it is only a few machine, maybe just do it by hand or commandline "grep". – John Siu Dec 10 '12 at 11:39
but I need to check more 100 files , and I need to match exactly to the RULE: [NUMBERS]||[letter]HOSTNAME[NUMBERS]||[letter] – yael Dec 10 '12 at 11:50
Understand. If possible, post the script. Then someone maybe able to modify it for you. Or if it is a stand script come with distribution, post the path so we can look it up in our machine. – John Siu Dec 10 '12 at 12:02
up vote 0 down vote accepted

Use fowlling line to print matching string

perl -i -pe 'next if /^ *#/; if ($_ =~ /(\b|[[:^alnum:]])$ENV{previos_machine_name}(\b|[[:^alnum:]])/) { print STDOUT $_; }'  file

If it print out empty, that means no old hostname exist.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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