Kenton Brede wrote:
>
> I've cobbled some code together that will allow me to parse a file
> snarfing 4 lines which consist of server name and Daily output of
> bandwith usage. I've pasted an example of what I have at the bottom of
> this mail. If anyone would like to take the time to show me how it
> should really be done I would apreciate it. I'm not exactly a
> programming wonder and trying to learn.
James covered a lot of good points so I won't go over them again. When
I see groups of input lines I usually think: "Can I modify the Input
Record Separator to capture the whole group?" If you can do that then
you can use a while loop to read one group at a time and not have to
slurp the entire file to process it.
#!/usr/bin/perl
use warnings;
use strict;
$/ = "\n* ";
while ( <DATA> ) {
next unless /Daily/;
chomp;
s/\A(.+?)^//sm and my $name = $1;
s/\A.+(?=^.*?Daily)//sm;
print "* $name$_";
}
__DATA__
* Leo
# Bandwidth Usage
eth0 Total for 106.95 days:
RX 307.28 MB
TX 768.05 MB
eth0 Daily average:
RX 2.87 MB
TX 7.18 MB
* Buffy2
# Bandwidth Usage
eth0 Total for 14.70 days:
RX 141.28 MB
TX 2.03 MB
eth0 Daily average:
RX 9.61 MB
TX 0.14 MB
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>