Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Parsing a non-formatted record text file

by TStanley (Canon)
on Feb 17, 2009 at 16:29 UTC ( #744472=note: print w/replies, xml ) Need Help??

in reply to Parsing a non-formatted record text file

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;

People sleep peaceably in their beds at night only because rough men stand ready to do violence on their behalf. -- George Orwell

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://744472]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2021-01-21 08:40 GMT
Find Nodes?
    Voting Booth?