use strict; use warnings; use Getopt::Std; use English; use Readonly; Readonly::Scalar my $ADDRESS => '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'; Readonly::Scalar my $FIELD => '[1-9]?[0-9]' .'|1[0-9][0-9]' .'|2[0-4][0-9]' .'|25[0-5]' ; my %opts; getopt('io',\%opts); die usage() if (!exists $opts{i} or !exists $opts{o}); open my $DEVICES_IN, '<', $opts{i} or die "Cannot open $opts{o} for input\n"; open my $DEVICES_OUT, '>', $opts{o} or die "Cannot open $opts{o} for output\n"; $OUTPUT_FIELD_SEPARATOR = ", "; my $line_count; my @fields = (); DEVICE: while (my $line = <$DEVICES_IN>) { if ((my $device) = $line =~ /^Device ID:\s(.+)$/) { $device =~ s/\s*$//; $line_count = 1; if (@fields) { local $OUTPUT_FIELD_SEPARATOR = ", "; print {$DEVICES_OUT} @fields; print {$DEVICES_OUT} "\n"; } @fields = (); $fields[0] = $device; next DEVICE; } if ($line_count++ > 15) { next DEVICE; } (my $address) = $line =~ /^(?:IP\s)?address:\s+($ADDRESS)/i; if ($address) { push @fields, $address; next DEVICE; } (my $platform) = $line =~ /Platform:\s(.+)$/; if ($platform) { push @fields, $platform; next DEVICE; } } # Output final device { local $OUTPUT_FIELD_SEPARATOR = ", "; print {$DEVICES_OUT} @fields; print {$DEVICES_OUT} "\n"; } sub usage { die "Usage:" ." achilder -i *input_file* -o *output_file*\n\n"; ; }