Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #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":



  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (4)
    As of 2015-07-03 23:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (57 votes), past polls