in reply to Extracting data from each line that matches a email address from a Log file (Tab delimited)

and to complete the suite - a version using DBI. Note that there is a "cheat" here in that the first line of the sample data has been omitted and, due to the # at the start of the header line the data field's name is '# Date'.

use strict; use warnings 'all'; use DBI; # Create the sample file open outFile, '>', 'sample.csv'; print outFile
'Date Time client-ip Client-hostname Partner-Name Serve +r-hostname server-IP Recipient-Address Event-ID MSGID +Priority Recipient-Report-Status total-bytes Number-Recipien +ts Origination-Time Encryption service-Version Linked-MSG +ID Message-Subject Sender-Address 2005-9-10 0:0:16 GMT - - - storming - Someoneg@ao +l.com 1027 2433A69xxxxxxxxxxxxxxxx795006DB02DADF78@storming.Dom +ain.name1 0 0 11927 1 2005-9-10 0:0:16 GMT 0 - + c=US;a= ;p=AMSCAN;l=storming-050910000016Z-212788 Fw: Hey Ugly l +ine expansion and re-offer EX:/O=org/OU=Site/CN=RECIPIENTS/CN=Ause +r - 2005-9-10 0:0:16 GMT - - - storming - c1r3ai4g@ao +l.com 1019 2433A690xxxxxxxxxxxxxxxx5006DB02DADF78@storming.Doma +in.name1 0 0 11927 1 2005-9-10 0:0:16 GMT 0 - + - Fw: Hey Ugly line expansion and re-offer - - 2005-9-10 0:0:16 GMT - - - storming - c1r3ai4g@ao +l.com 1025 2433A6xxxxxxxxxxxxxxxx95006DB02DADF78@storming.Domai +n.name1 0 0 11927 1 2005-9-10 0:0:16 GMT 0 - +- Fw: Hey Ugly line expansion and re-offer - - 2005-9-10 0:0:16 GMT - - - storming - c1r3ai4g@ao +l.com 1024 2433A690Fxxxxxxxxxxxxxxxx6795006DB02DADF78@storming. +Domain.name1 0 0 11927 1 2005-9-10 0:0:16 GMT 0 +- - Fw: Hey Ugly line expansion and re-offer - - 2005-9-10 0:0:17 GMT - - - storming - c1r3ai4g@ao +l.com 1033 2433Axxxxxxxxxxxxxxxx428E5EE4C6795006DB02DADF78@stor +ming.Domain.name1 0 0 11927 1 2005-9-10 0:0:16 GMT +0 - - Fw: Hey Ugly line expansion and re-offer Auser@Doma +in.name - 2005-9-10 0:0:17 GMT - - - storming - c1r3ai4g@ao +l.com 1020 2433A69xxxxxxxxxxxxxxxx95006DB02DADF78@storming.Doma +in.name1 0 0 11927 1 2005-9-10 0:0:16 GMT 0 - + - Fw: Hey Ugly line expansion and re-offer Auser@Domain.name + -
'; close outFile; # The real sample code my $dbh = DBI->connect(qq{DBI:CSV:csv_sep_char=\\\t}) or die "Cannot connect: " . $DBI::errstr; my $sth = $dbh->{'csv_tables'}->{'info'} = { 'file' => 'sample.csv'}; $sth = $dbh->prepare("SELECT * FROM sample.csv") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die "Cannot execute: " . $sth->errstr(); while (my $row = $sth->fetchrow_hashref) { print("$row->{'Date'}, $row->{'Time'}, $row->{'Recipient-Address'}, +$row->{'Message-Subject'}, $row->{'Sender-Address'}, \n"); } $sth->finish(); $dbh->disconnect();

Prints:

2005-9-10, 0:0:16 GMT, Someoneg@aol.com, Fw: Hey Ugly line expansion a +nd re-offer, EX:/O=org/OU=Site/CN=RECIPIENTS/CN=Auser, 2005-9-10, 0:0:16 GMT, c1r3ai4g@aol.com, Fw: Hey Ugly line expansion a +nd re-offer, -, 2005-9-10, 0:0:16 GMT, c1r3ai4g@aol.com, Fw: Hey Ugly line expansion a +nd re-offer, -, 2005-9-10, 0:0:16 GMT, c1r3ai4g@aol.com, Fw: Hey Ugly line expansion a +nd re-offer, -, 2005-9-10, 0:0:17 GMT, c1r3ai4g@aol.com, Fw: Hey Ugly line expansion a +nd re-offer, Auser@Domain.name, 2005-9-10, 0:0:17 GMT, c1r3ai4g@aol.com, Fw: Hey Ugly line expansion a +nd re-offer, Auser@Domain.name,

Perl is Huffman encoded by design.
  • Comment on Re: Extracting data from each line that matches a email address from a Log file (Tab delimited)
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Extracting data from each line that matches a email address from a Log file (Tab delimited)
by jZed (Prior) on Oct 28, 2005 at 15:13 UTC
    my $sth = $dbh->{'csv_tables'}->{'info'} = { 'file' => 'sample.csv'};
    $sth = $dbh->prepare("SELECT * FROM sample.csv")
    The "my $sth =" on the first line is wrong. And the SQL in the second line should be either q{SELECT * FROM "sample.csv"} or q{SELECT * FROM info}. The first uses quotes to delimit the table name (thus allowing the forbidden period) and the second skirts the issue by using the alias to the filename you created with csv_tables.