Operating System - HP-UX
1752807 Members
6026 Online
108789 Solutions
New Discussion юеВ

Perl Help - strip unwanted characters

 
SOLVED
Go to solution
Paul D. Simpson
Frequent Advisor

Perl Help - strip unwanted characters

Hi All,
I have a Perl script with the following line:

if ( $line =~ /( ^Subject:.*? )( To: )/xms ) {
print OUTFIL $1, "\n";
}

WHich grabs the subject line of an email and writes it to a file.

I have a charset statement in the subject line in 2 - 3 places that I want to strip out

Subject line looks like:
Subject: =?ISO-8859-1?Q?Customer:_1111111,_Customer_PO:_123456789,_Order:_161318(?=
=?ISO-8859-1?Q?Added_lines_43)__06-12-2?=

How do I strip out the "=?ISO-8859-1?Q?" from the line?

Thanks in advance

Paul
2 REPLIES 2
James R. Ferguson
Acclaimed Contributor
Solution

Re: Perl Help - strip unwanted characters

Hi Pault:

Since this thread is really a continuation of your earlier thread here:

http://forums13.itrc.hp.com/service/forums/questionanswer.do?threadId=1347151

...I'm going to build from it. Using this as a test file:

# cat testinput
Subject: =?ISO-8859-1?Q?Customer:_1111111,_Customer_PO:_123456789,_Order:_161318(?=
=?ISO-8859-1?Q?Added_lines_43)__06-12-2?=
To: me

...your amended script might look like:

# cat ./mymail
#!/usr/bin/perl
use strict;
use warnings;
my ( $line, $subject );
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 ) {
( $subject = $1 ) =~ s/\Q=?ISO-8859-1?Q?\E//gxms;
print "$subject\n";
}
}
}
close OUTFIL;
exit;

...which yields:

# ./mymail testinput
Subject: Customer:_1111111,_Customer_PO:_123456789,_Order:_161318(?=
Added_lines_43)__06-12-2?=

I left the original logic to extract the contents of the whole subject. Then, since the string you wanted to remove occurred in several places I chose to substitute it for a null string (globally) rather than complicate the match expression and snip multiple pieces. Since the string in question has regular expression metacharacters in it, I escaped the string with '\Q...\E'. Once the $subject is collected and cleaned up we print it.

Once again, for ease of debugging, I let output go to STDOUT.

Regards!

...JRF...
Paul D. Simpson
Frequent Advisor

Re: Perl Help - strip unwanted characters

James... thanks again! Works perfectly.
Paul