0

According to the following Perl command

( this command part of ksh script )

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

 previos_machine_name=linux1a
 new_machine_name=Red_Hat_linux1a

 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

   previos_machine_name=linux1a

   new_machine_name=Red_Hat_linux1a

   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
   .
   .
   .
   .
6
  • Are we talking a few (less than 10) machines or a lot here?
    – John Siu
    Commented Dec 10, 2012 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
    Commented Dec 10, 2012 at 11:33
  • Well, if it is only a few machine, maybe just do it by hand or commandline "grep".
    – John Siu
    Commented Dec 10, 2012 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
    Commented Dec 10, 2012 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
    Commented Dec 10, 2012 at 12:02

1 Answer 1

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .