http://www.perlmonks.org?node_id=698807


in reply to parsing text files continued

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' } };

--
altblue.