On May 24, 2:21 am, "robertchen@gmail.com"
<robertchen
@gmail.com> wrote:
> I have a long list of host/node configuration file need to parsed and
> print in a CGI table.
> I want to sort them by group and then by address, by on_off, polling.
> Please help me how to do it. The node name is unique. If just sort by
> name, it is easy...
> begin node
> name ge17.net.domain.com
> on_off on
> group CISCO-SWITCH
> suppress no
> auto_delete yes
> read_community OJ3yEgsBQ7
> write_community public
> address 10.9.1.118
> port 161
> snmp_version 2
> engineid 0
> auth_protocol 0
> is_key_ok 1
> error_status 0
> security_level 0
> end node
> begin node
> name v255.net.domain.com
> on_off on
> group CISCO-SWITCH
> suppress no
> auto_delete yes
> read_community OJ3yEgsBQ7
> write_community public
> address 10.9.255.3
> port 161
> snmp_version 2
> engineid 0
> auth_protocol 0
> is_key_ok 1
> error_status 0
> security_level 0
> end node
I don't see anything named "polling" here, but admittedly I don't know
much about host/node configurations...
Regardless, here's an example you could study and hopefully modify for
your own needs...
#!/usr/bin/env perl
use strict;
use warnings;
my @nodes;
#Change the input record separator so that the
#readline operator reads newline-separated records
local $/ = "";
while (my $record = <DATA>){
chomp $record;
my %node;
#find and store each of the pieces of info we care about
for my $key (qw/on_off address group/) {
($node{$key}) = ($record =~ /$key (.*)/);
}
#store the entire record, to be printed later
$node{record} = $record;
#add this node to our list.
push @nodes, \%node;
}
#obtain a list of sorted nodes,
#going by group, address, and on_off
my @sorted_nodes = sort {
$a->{group} cmp $b->{group} or
$a->{address} cmp $b->{address} or
$a->{on_off} cmp $b->{on_off}
} @nodes;
#print out each node's record
for my $node_ref (@sorted_nodes) {
print $node_ref->{record} . "\n\n";
}
__END__
One obvious thing you're going to want to change is the way the
addresses are sorted. The above does a simple ASCIIbetical
comparison. Which means, from your example, 10.7.255.144 comes before
10.7.255.15. If that's not what you want, do some manual parsing on
those values and sort them better...
Hope this helps,
Paul Lalli