Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Parsing Data logger files ...

by RedTussock (Acolyte)
on Oct 07, 2012 at 00:40 UTC ( #997649=note: print w/ replies, xml ) Need Help??


in reply to Re: Parsing Data logger files ...
in thread Parsing Data logger files ...

Sorry the previous comment was a bit naff ... where I am at is

#!/usr/bin/perl use warnings; use strict; opendir DH, "/a/b/c/perl/dl_parser/down/" or die "Couldn't open the d +irectory +: $!"; while ($_ = readdir (DH)) { $/ = "\n1="; my @fields = split /\n/; s/^[1-6]=// for @fields; print +(join ',', @fields[0 .. 5]), "\n"; } exit
which gets me a list of files and 5 other lines ... :-) like
101130.TXT,,,,, Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. 240429.TXT,,,,, Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. 110208.TXT,,,,, Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. 081221.TXT,,,,, Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. 100807.TXT,,,,, Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. Use of uninitialized value in join or string at lineJumper.pl line 11. 081219.TXT,,,,,
So getting close ... need to read each file ... maybe split into modules one that reads the file names from the directory, and one that process's them?. .. then build some interface to ask for the directory and maybe a file name to print the records too.


Comment on Re^2: Parsing Data logger files ...
Select or Download Code
Re^3: Parsing Data logger files ...
by mbethke (Hermit) on Oct 07, 2012 at 00:51 UTC

    The simplest solution would be to call choroba's script as "munge.pl /some/where/*" and let the shell do the directory stuff. "<>" will automatically try to read everything in @ARGV so you don't have to deal with it yourself.

    Otherwise you're on the right track with readdir but you forgot to actually open each individual file.

      Perfect ... and a lesson in spotting the tree in the forest. Thank you both ... I guess now all I have to do is print it too a file.
      Something like
      #!/usr/bin/perl use warnings; use strict; $/ = "\n1="; while (<>) { my @fields = split /\n/; s/^[1-6]=// for @fields; open (MYFILE, '>/a/b/c/perl/dl_parser/logData.txt'); print MYFILE "+(join ',', @fields[0 .. 5]), "\n""; close (MYFILE); }

        Consider the following:

        #!usr/bin/perl use strict; use warnings; $/ = "\n1="; open my $fh, '>', '/a/b/c/perl/dl_parser/logData.txt' or die $!; while (<>) { my @fields = split /\n/; s/^[1-6]=// and s/\s+$// for @fields; print $fh ( join ',', map qq/"$_"/, @fields[ 0 .. 5 ] ) . "\n"; } close $fh;

        For each csv line, your script will open, write to, and then close the logData.txt file. When done, that file will have only one line, viz., the last one written to it. The above will write all csv lines to your file, since the open and close statements are outside the while loop. Additionally, note that open is using a lexically-scoped file handle (my $fh) and that it is a three-argument open.

        Have added two items to the script. The first is an additional substitution, since there was a space at the end of the last three fields. The second, a map statement, encloses each field within quotations, since the first data field contains a comma (remove the map qq/"$_"/, if you want the date and time to be treated as separate csv fields).

        Hope this helps!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997649]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2014-12-19 13:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (83 votes), past polls