Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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]
[stevieb]: shmem thanks for the 'insight' :P
[shmem]: good thing that Sun already took "OpenWindows", otherwise I'd not stop to shudder imagining an "OpenWindows" from MS
[shmem]: more garbage in, more garbage out that would be
[stevieb]: I found that win10 broke a C# library I was using for one project while enhancing tests for a Perl dist, which breaks other Perl dists, and I'm about to throw my hands up on berrybrew. win2k12 broke one thing, win10 breaks something...
[stevieb]: ...unrelated which requires replacing a lot of code and a whole lib. I'm about to go nix only ffs
[shmem]: stevieb: what you're doing sounds afwully complex. Too much for me this evening to provide brighter insight ;-)
[stevieb]: I don't even own a Windows computer. Both my girl and I have a laptop each with Linux. I'm supporting Windows in some of my projects and I can't even guage whether it's worth it or not.
[stevieb]: shmem It's something I desired to have years ago, which is why I took over berrybrew. Cross-platform build/test automation locally, or over the network Test::BrewBuild
[shmem]: sounds good.

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2017-03-28 22:04 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (342 votes). Check out past polls.