Thursday, September 21, 2006

Perl regex to parse out the swinstall.hosts from /.sw/defaults.hosts

Perl regex to parse out the swinstall.hosts from /.sw/defaults.hosts The string can be like this

swinstall.hosts = {ws01 s003 s002 s001 a7altsrv a7server}

or

swinstall.hosts = {
s004
s001sdb1
s003
s002p1
s002
ws01
s001p1
s001
a7server
}

Ie. The code needs to deal with line breaks

undef $/;
open(DH, "</.sw/defaults.hosts") or die "unable to open default.hosts\n";
$file = <dh>;
close(DH);
$file =~ m/
swinstall\.hosts # Match the beginning of the hosts we want
(?:.*?) # followed by a space or not. Minimal match
= # followed by an '='
(?:.*?) # followed by a space or not. Minimal match
{ # followed by the open brace to begin the list of hosts
(.*?) # followed by some characters which are the
# hostnames saved into a back reference
} # followed by the close brace
/six;
print $1 . "\n";

No comments: