GlenM wrote:
Okay;

I am sure that someone out there has done this before - I *think* I am
on the right track.

I have a directory full of emails. What I would like to do is read
each file in, then parse them into a CSV style file.

Example:


#!/usr/bin/perl

use warnings;
use strict;

open FILE , "/home/gmillard/SentMail/YourSatSetup.txt" or die $!;
my $linenum =1;

while (<FILE>) {
        print "|", $linenum++;
        print"$_" ;
}


So, the first line of the CSV file is
|1From - Sun Feb 21 11:40:01 2010|2X-Mozilla-Status: 0001|3X-Mozilla-
Status2: 00000000|4X-Gmail-Received:
58fa0ec68ca9975c1d187ceadc0ad3aeb1026134
<truncated>

Use opendir/readdir, an array and join(), for example something like this:

...
my @csv;
opendir(my $dfh, '/home/gmillard/SentMail') or die($!);
while((my $fname = readddir($dfh))) {
    # ignore unwanted files
    next if($fname =~ /^\./o || $fname !~ /\.txt$/oi);

    my $fullname = '/home/gmillard/SentMail/' . $fname;

    # Ignore non-files
    next if(!(-f $fullname));

    my @columns;
    # Open file and read it into @colunms
    open(my $fh, '<', $fullname) or die($!);
    my $linecnt = 1;
    while((my $line = <$fh>) {
        # Add every line to @columns
        chomp $line;
        push @columns, "$linecnt$line";
        $linecnt++;
    }
    close($fh);
    # join the whole @columns into a single scalar and
    # push that into @csv
    push @csv, join('|', @columns);
}
closedir($dfh);

# Output data to file
open(my $ofh, '>', 'output.csv') or die($!);
foreach my $csvline (@csv) {
  print $ofh "$csvline\n";
}
close($ofh);
...


This reads the whole lot into memory (which is fast if you got enough). If you have a BIG Mailfolder, you might have to think of a slightly different strategy.

Also, your CSV layout will brake if even a single mail contains a pipe sign. I leave it up to the reader to find a workaround (typing "perldoc perlre" at the command prompt might give you a hint, though).

LG
Rene

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to