Re: Help: Variables problem
- From: Ted Zlatanov <tzz@xxxxxxxxxxxx>
- Date: Fri, 29 Aug 2008 10:21:52 -0500
On Fri, 29 Aug 2008 22:44:18 +0800 Amy Lee <openlinuxsource@xxxxxxxxx> wrote:
AL> I'm writing a script to parse the contents of /proc/loadavg file. And the
AL> file is like this:
AL> 0.22 0.11 0.03 1/92 2653
AL> And I hope my script output is like this:
AL> 1 minute loadavg: 0.22
AL> 5 minutes loadavg: 0.11
AL> 10 minutes loadavg: 0.03
AL> Running processes: 1 processes
AL> Total processes: 92 processes
AL> And there's my codes:
AL> open LOADAVG, "<", "/proc/loadavg"
AL> or die RED "Can't open /proc/loadavg. $!";
AL> while (<LOADAVG>)
AL> {
AL> <LOADAVG> =~ /(\d+)\.(\d+)\s+(\d+)\.(\d+)\s+(\d+)\.(\d+)\s+(\d+)\/(\d+)\s+\d+/;
AL> print "1 minute loadavg: $1.$2\n";
AL> print "5 minutes loadavg: $3.$4\n";
AL> print "10 minutes loadavg: $5.$6\n";
AL> print "Running processes: $7 processes\n";
AL> print "Total processes: $8 processes\n";
AL> }
AL> close LOADAVG;
Use the split ' ' syntax to get individual fields (you'll need a further
split for field 4, of course). You can also consider Regexp::Common to
extract numbers.
Or install the CPAN Linux::loadavg module.
Ted
.
- References:
- Help: Variables problem
- From: Amy Lee
- Help: Variables problem
- Prev by Date: Re: Perl Newbie
- Next by Date: Re: matching file name without digits
- Previous by thread: Help: Variables problem
- Index(es):