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
Results:
$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'
}
};
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.