Hi,
I have a log file containing data on emails sent. Looks a bit like this for one email:
Content-Type: text/plain;
charset="UTF-8"
Date: 12 Jun 2008 14:04:59 +0100
From: from@email.com
Subject: xcf4564xzcv
To: recip@email.co.uk
Size = 364 Jun 12 14:04 smtp_234sldfh.tmp
I need to take the subject, date, time, size and To: and stick it in an output file in the following format
1,recip@email.co.uk,1,,,1,xcf4564xzcv,1,12 Jun 2008,14:04:59,1,,364
How can I do this in Shell (AIX)... sed? grep? Here is the perl script I wrote:
open(READLOGFILE, "C:\\temp\\email.log") or die("Failed to open file");
open(WRITELOGFILE, ">>C:\\temp\\emailstats.log") or die("Failed to open file");
$numRecords = 0;
$emailSize = 0;
$foundSize = 0;
$foundEmail = 0;
$foundSubject= 0;
$foundDate = 0;
while($line = <READLOGFILE>) {
if($line =~ /^Subject\:\s(\S*)/ ) {
$subject = $1;
$foundSubject = 1;
}
if($line =~ /^Date\:\s(.*)\s(\d\d\:\d\d\:\d\d)\s(.*)/) {
$date = $1;
$foundDate = 1;
$time = $2;
}
if($line =~ /^To\:\s(\S*\@\S*)/ ) {
$to = $1;
$foundEmail = 1;
}
if($line =~ /^Size\s\=\s(\d*)/) {
$foundSize = 1;
$emailSize = $1;
}
if(($foundSubject + $foundEmail + $foundSize + $foundDate) == 4) {
print WRITELOGFILE "1,".$to.",1,,,1,".$subject.",1,".$date.",".$time.",1,,".$emailSize."\n";
$numRecords = $numRecords + 1;
$totalSize = $totalSize + $emailSize;
$emailSize = 0;
$foundSize = 0;
$foundEmail = 0;
$foundSubject= 0;
$foundDate = 0;
}
}
print "\nProcessing Complete";