- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Help to extract lines from email file in Perl
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2009 09:49 AM
тАО06-11-2009 09:49 AM
Thanks in advance for the help.
I have a Perl script that is grabbing headers from email messages & logging them to a file... Unfortunately, my sender switched the mechanism he uses to send these emails & now there seems to be a newline on some of the data I am trying to grab (Subject is the main culprit). I'd like to make my script grab everything from "Subject:" up to right before "To:", and "To:" up to just before "Cc:", "Cc:" up to just before "Message-ID"
Can anyone help please?
My script contains:
#!/opt/perl/bin/perl -w
while ($line=
my @now = localtime();
my $timestamp = sprintf("%04d-%02d-%02d",$now[5]+1900, $now[4]+1, $now[3]);
my $OUTPUT = "/home/share/acks/order_acks_$timestamp.txt";
open (OUTFIL, ">>$OUTPUT") || die "Cannot open $OUTPUT file";
if ($line =~ /^To:/){
chomp $line;
print OUTFIL "$line \n";
}
if ($line =~ /^[Cc]c:/){
chomp $line;
print OUTFIL "$line \n";
}
if ($line =~ /^Subject:/){
chomp $line;
print OUTFIL "$line \n\n";
}
}
close OUTFIL;
exit;
(Yes, I'm new to Perl)
The file I'm grabbing from looks like:
( I added ###<---->###### tages to highlight what I need)
From Sales.Order.Ack.Exceptions@company.com Wed Jun 10 15:56:21 2009
Return-Path:
Received: from smtp02.company.com (chn-sv-exft02.microchip.com [99.99.99.18])
by newton.company.com (8.9.3 (PHNE_35484)/8.9.3) with ESMTP id PAA03316;
Wed, 10 Jun 2009 15:56:21 -0700 (MST)
Received: from CHN-VM-STRMSRV.company.com ([99.99.99.126]) by smtp02.company.com with Microsoft SMTPSVC(6.0.3790.1830);
Wed, 10 Jun 2009 15:56:20 -0700
Date: Wed, 10 Jun 2009 15:56:20 -0700 (PST)
From: Your Order Acknowledgement
Reply-To: Sales.Order.Ack.Exceptions@company.com
####<-- SUBJECT GRAB START HERE-->#####
Subject: =?ISO-8859-1?Q?Customer:_110557,_Customer_PO:_123456789,_Order:_132456(?=
=?ISO-8859-1?Q?Changed_lines_41)__06-10?=
####<-- SUBJECT GRAB END HERE-->#####
####<-- TO GRAB START HERE-->#####
To: customer456@yahoo.com
####<-- TO GRAB END HERE-->#####
####<-- CC GRAB START HERE-->#####
####<-- CC GRAB END HERE-->#####
Message-ID: <7081244674580691@company.com>
MIME-Version: 1.0
Content-Type: MULTIPART/MIXED; BOUNDARY="6978513-22148-1244674580=:708"
X-OriginalArrivalTime: 10 Jun 2009 22:56:20.0044 (UTC) FILETIME=[AB49F8C0:01C9EA1E]
--6978513-22148-1244674580=:708
Content-Type: TEXT/PLAIN; CHARSET=ISO-8859-1
Content-Transfer-Encoding: QUOTED-PRINTABLE
We thank you for your order. An Order Acknowledgement is attached.=0A=0AIf=
you need further information...
Solved! Go to Solution.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2009 10:52 AM
тАО06-11-2009 10:52 AM
SolutionIf the lines are truly multiple ones, we could do something like this:
# cat ./mymail
#!/usr/bin/perl
use strict;
use warnings;
my $line;
my @now = localtime();
my $timestamp
= sprintf( "%04d-%02d-%02d", $now[5] + 1900, $now[4] + 1, $now[3] );
my $OUTPUT = "home/share/acks/order_acks_$timestamp.txt";
#->open (OUTFIL, ">>$OUTPUT") || die "Cannot open $OUTPUT file";
{
local $/ = undef;
while ( $line = <> ) {
chomp $line;
if ( $line =~ /( ^To:.*? )( [Cc]c:|Message-ID )/xms ) {
print $1, "\n";
}
if ( $line =~ /( ^[Cc]c:.*? )( Message-ID )/xms ) {
print "$line \n";
}
if ( $line =~ /( ^Subject:.*? )( To: )/xms ) {
print $1, "\n";
}
}
}
close OUTFIL;
exit;
...
We slurp the whole file into one big record. Then we capture the pieces we want by matching the across any newline characters with '/s' regex modifier to allow '.' to match a newline.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2009 12:59 PM
тАО06-11-2009 12:59 PM
Re: Help to extract lines from email file in Perl
It is definitely sending the correct output to the screen, but does not seem to be writing to the output file.
FYI: This is being used as an alias redirect for sendmail, such that incoming mail for address "ack" is piped to this script. Not sure if that makes a difference.
Paul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2009 01:13 PM
тАО06-11-2009 01:13 PM
Re: Help to extract lines from email file in Perl
> It is definitely sending the correct output to the screen, but does not seem to be writing to the output file.
Yes, that's because for ease of testing I dropped your output filehandle leaving STDOUT as the default, selected one.
Instead of:
# print $1, "\n";
change to:
# print OUTFIL $1, "\n";
(or):
# print OUTFIL "$1\n";
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2009 01:29 PM
тАО06-11-2009 01:29 PM
Re: Help to extract lines from email file in Perl
Thanks... I should have caught that... I had the same problem when I first wrote the script.
Thanks again
-Paul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2009 01:30 PM
тАО06-11-2009 01:30 PM