Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Assuming you always have the same fields for each records, here's a lazy way to obtain your CSV:
$ perl -F, -lane '@F % 2 and push @D, [@F] or push @{$D[-1]}, pop @F; +}{ $,=q{,}; print @{$_} for @D' input.txt
... running it against your sample input, you will get:
1/3/2007 12:20:01 AM,12.588309,9.432586,20:0.196329,7.418672,3.616305, +2.066482,6.873061,0.784989,1.859894,3.249620,0.450952,0.305768,0.8234 +02 1/3/2007 12:49:22 AM,10.958312,13.644527,41:0.483233,7.027840,4.222601 +,0.305821,7.443877,1.552915,1.202711,5.285398,0.233119,0.425521,0.560 +862
I repeat, this is very lazy as it assumes you always have the same fields, in the same order ;-)

A better/fancier way would be to gather everything in some hashes or something, and then use some "proper" formatter to dump results as CSV. Here's a way to obtain your hash:

$ perl -MData::Dumper -F, -lane '@F % 2 and ($k)=@F or push @{$D{$k}}, + @F; }{ $D{$_} = {@{$D{$_}}} for keys %D; print Dumper \%D' input.txt
$VAR1 = { '1/3/2007 12:49:22 AM' => { 'ClientAdd' => '1.552915', 'CMALoad' => '1.202711', 'SearchDelete' => '0.305821', 'CMASave' => '5.285398', 'ClientDelete' => '0.425521', 'CMADelete' => '0.233119', 'Login' => '10.958312', 'SearchCount' => '41:0.483233', 'SearchDetails' => '7.443877', 'Logout' => '0.560862', 'SearchResults' => '7.027840', 'SearchSave' => '4.222601', 'SearchLoad' => '13.644527' }, '1/3/2007 12:20:01 AM' => { 'ClientAdd' => '0.784989', 'CMALoad' => '1.859894', 'SearchDelete' => '2.066482', 'CMASave' => '3.249620', 'ClientDelete' => '0.305768', 'CMADelete' => '0.450952', 'Login' => '12.588309', 'SearchCount' => '20:0.196329', 'SearchDetails' => '6.873061', 'Logout' => '0.823402', 'SearchResults' => '7.418672', 'SearchSave' => '3.616305', 'SearchLoad' => '9.432586' } };


In reply to Re: parsing text files continued by AltBlue
in thread parsing text files continued by grashoper

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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?

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others cooling their heels in the Monastery: (6)
    As of 2024-07-12 21:43 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found

      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.