I need to iterate through my file which has a date time value and seperate each date time into a new row, I am not sure how to pull this off, each file has same fields and 15 of them make up a row for 1 record for excel. example of data is in this block. code I have so far is below it..
1/3/2007 12:20:01 AM Login,12.588309 SearchLoad,9.432586 SearchCount,20:0.196329 SearchResults,7.418672 SearchSave,3.616305 SearchDelete,2.066482 SearchDetails,6.873061 ClientAdd,0.784989 CMALoad,1.859894 CMASave,3.249620 CMADelete,0.450952 ClientDelete,0.305768 Logout,0.823402 1/3/2007 12:49:22 AM Login,10.958312 SearchLoad,13.644527 SearchCount,41:0.483233 SearchResults,7.027840 SearchSave,4.222601 SearchDelete,0.305821 SearchDetails,7.443877 ClientAdd,1.552915 CMALoad,1.202711 CMASave,5.285398 CMADelete,0.233119 ClientDelete,0.425521 Logout,0.560862
my code so far..
#!/usr/bin/perl use strict; my $i=0; my $return="\n"; ( @ARGV == 1 and -d $ARGV[0] ) or die "Usage: $0 dir_name > out.csv\n" +; my $dirname = shift; opendir( DIR, $dirname ) or die "$dirname: $!"; my ( $firstfile, @files ) = grep /.+\.txt$/, readdir DIR; open( F, $firstfile ) or die "$firstfile: $!"; my ( @names, @values ); my ( $basename ) = ( $firstfile =~ /(.+)\.txt$/ ); my $date = <F>; while (<F>) { chomp; while ($i++<14) { my ( $n, $v ) = split /,/, $_, 2; push @names, $n; push @values, $v; } continue push @names, $return; my ( $n, $v ) = split /,/, $_, 2; push @names, $n; push @values, $v; } close F; print join( ",", "sourcefile", "date", @names ), "\n"; print join( ",", $basename, $date, @values ), "\n"; for my $file ( @files ) { ( $basename ) = ( $file =~ /(.+)\.txt$/ ); if ( open( F, $file )) { ( $date, @values ) = <F>; chomp $date; chomp @values; s/.+?,// for ( @values ); # delete the "name," parts print join( ",", @values ), "\n"; close F; } else { warn "$file: $!\n"; } }

In reply to parsing text files continued by grashoper

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!
  • 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:
    & &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.