Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re: Parsing a non-formatted record text file by TStanley
in thread Parsing a non-formatted record text file by TStanley

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-03-29 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found