Here is the final code:
#!C:\perl\bin\perl -w
use strict;
my @records = ();
my $inrecord = 0;
my $rxRecStart = qr{^System:};
my $rxRecStop = qr{^CPU\sTTY};
my $recordstring = q{};
my ($system,$date,$time,$procsleep,$procrun,$null,$load,$user,$nice,$s
+ys,$idle,$block,$swait,$intr,$ssys,$real,$virtual,$free);
my $IRS = q/+/ x 50;
my @record =();
open INPUTFILE,"cpulist.txt"||die "Can not open: $!\n";
open TEMPOUT,'>',"tempout.txt"||die "Can not open: $!\n";
while (<INPUTFILE>){
next if m{^\s*$};
if(m{$rxRecStart}){
$inrecord = 1;
push @records,$recordstring if $recordstring;
$recordstring = $_;
}elsif(m{$rxRecStop}){
$inrecord = 0;
push @records, $recordstring if $recordstring;
$recordstring = q{};
}else{
$recordstring .= $_ if $inrecord;
}
}
close INPUTFILE;
foreach my $record (@records){
print TEMPOUT $record, q{+} x 50, qq{\n};
}
close TEMPOUT;
$/ = "$IRS\n";
open TEMPOUT,"tempout.txt"||die"Can not open: $!\n";
open CSVOUTFILE,'>',"output.csv"||die"Can not open output.csv: $!\n";
print CSVOUTFILE "System,Date,Time,Processes Sleeping,Processes Runnin
+g,Load,User,Nice,Sys,Idle,Blocking,SWait,Intr,SSys,Real Memory(K),Vir
+tual Memory(K),Free Memory(K)\n";
while(<TEMPOUT>){
chomp;
@record = split /\n/;
if($record[0]=~m/^System:\s+(\w+)\s+\w{3}\s+(\w{3}\s+\d{1,2})\s+(\d{
+1,2}:\d{1,2}:\d{1,2})\s+\d{4}$/){
$system = $1;
$date = $2;
$time = $3;
}
if($record[2]=~m/^\d+\s+processes:\s+(\d+)\s+sleeping,\s+(\d+)\s+run
+ning$/){
$procsleep = $1;
$procrun = $2;
}
($null,$load,$user,$nice,$sys,$idle,$block,$swait,$intr,$ssys) = spl
+it /\s+/,$record[5];
if($record[6]=~m/^Memory:\s+(\d+)K\s+\(\d+K\)\s+real,\s+(\d+)K\s+\(\
+d+K\)\s+virtual,\s+(\d+)K\s+free(.*)$/){
$real = $1;
$virtual = $2;
$free = $3;
}
print CSVOUTFILE "$system,$date,$time,$procsleep,$procrun,$load,$use
+r,$nice,$sys,$idle,$block,$swait,$intr,$ssys,$real,$virtual,$free\n";
}
close TEMPOUT;
close CSVOUTFILE;
TStanley
--------
People sleep peaceably in their beds at night only because rough men stand ready to do violence on their behalf. -- George Orwell
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.