in reply to parsing files and modifying format
If that's the basic plan, I wouldn't bother with writing modified versions of every original file. Just output a single stream of data (store it all in a single file), having one line of text for each input file. Something like this:
(I'm assuming you want to keep track of the original file names where all of the output rows come from, as well as all the time-stamps.)#!/usr/bin/perl use strict; # you really should! ( @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 @files = grep /.+\.txt$/, readdir DIR; for my $file ( @files ) { my ( $basename ) = ( $file =~ /(.+)\.txt$/ ); if ( open( F, $file )) { my ( $date, @lines ) = <F>; chomp $date; chomp @lines; print join( ",", "sourcefile", $basename, "date", $date, @line +s ), "\n"; close F; } else { warn "$file: $!\n"; } }
Of course, if it turns out that all the files have the same number of lines, and the same set of names in their various "name,value" pairs, and these names are always in the same order, a better input for excel would have a single line at the top with the names, and then each following line would just have the values in the proper order:
(Not tested)#!/usr/bin/perl use strict; ( @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; 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( ",", $basename, $date, @values ), "\n"; close F; } else { warn "$file: $!\n"; } }
If you're not sure whether all the files have the same set of names in the same order, it would be easy enough to check for that before you try to create the single data stream for importing to excel.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: parsing files and modifying format
by grashoper (Monk) on Jul 18, 2008 at 06:17 UTC | |
by graff (Chancellor) on Jul 18, 2008 at 23:41 UTC | |
|
Re^2: parsing files and modifying format
by grashoper (Monk) on Jul 18, 2008 at 05:27 UTC | |
|
Re^2: parsing files and modifying format
by Anonymous Monk on Mar 17, 2009 at 18:02 UTC |