Reputation: 41
Currently I am greping 3rd line from matching pattern. In this case q33BDrP9007220
is a matching pattern and 3rd line will be printed, I want to print 4th and 5th line as well, but only if the pattern of 4th and 5th line matches the 3rd line pattern.And print those 3 email ids in one line separated by ','.
open (MYFILE,<$mailqdir);
while(<MYFILE>)
if(/(\w{14})/){
next unless \w{14})/ % 2;
$temp = scalar <MYFILE>;
$rf_id = $temp;
}
--------------Input-----------------
q33BDrP9007220 50153 Tue Apr 3 16:43 <[email protected]>
(Deferred: 451 4.2.1 mailbox temporarily disabled: paond.tndt)
<[email protected]>
<[email protected]>
<[email protected]>
q33BDrP9007220 50153 Tue Apr 3 16:43 <[email protected]>
(Deferred: 451 4.2.1 mailbox temporarily disabled: paond.tndt)
<[email protected]>
<[email protected]>
<[email protected]>
q33BDrP9007220 50153 Tue Apr 3 16:43 <[email protected]>
(Deferred: 451 4.2.1 mailbox temporarily disabled: paond.tndt)
<[email protected]>
<[email protected]>
<[email protected]>
Upvotes: 0
Views: 205
Reputation:
Here's how I would go
use strict;
use warnings;
my $file="input_file.txt";
open (FILE,"<$file") or die "Can't open $file: $!";
my $contents = do { local $/; <FILE> };
my @arr=split('q33BDrP9007220', $contents);
foreach my $ele(@arr)
{
$ele =~ s/([a-zA-Z0-9_.]+@[a-zA-Z]+\.[a-zA-Z]{2,4})//;
$ele =~ s/.*?([a-zA-Z0-9_.]+@[a-zA-Z]+\.[a-zA-Z]{2,4})/$1 ,/sg;
print $ele;
}
You can change the regex ([a-zA-Z0-9_.]+@[a-zA-Z]+\.[a-zA-Z]{2,4})
for email address with your own.
Upvotes: 0
Reputation: 241848
This example might help you:
#!/usr/bin/perl
use warnings;
use strict;
my @emails;
while (<DATA>) {
if (/^\w{14}\s/) {
<DATA>; # skip one line
output(@emails);
undef @emails; # forget emails
} elsif (/^\s+<([^>]+)>$/) {
push @emails, $1; # remember the email
}
}
# Print the last rememberd emails
output(@emails);
sub output {
print join(',', @_), "\n" if @_;
}
__DATA__
q33BDrP9007220 50153 Tue Apr 3 16:43 <[email protected]>
(Deferred: 451 4.2.1 mailbox temporarily disabled: paond.tndt)
<[email protected]>
<[email protected]>
<[email protected]>
q33BDrP9007220 50153 Tue Apr 3 16:43 <[email protected]>
(Deferred: 451 4.2.1 mailbox temporarily disabled: paond.tndt)
<[email protected]>
<[email protected]>
<[email protected]>
q33BDrP9007220 50153 Tue Apr 3 16:43 <[email protected]>
(Deferred: 451 4.2.1 mailbox temporarily disabled: paond.tndt)
<[email protected]>
<[email protected]>
<[email protected]>
Also, read Parsing the file perl here on StackOverflow - posted by your colleague or classmate maybe?
Upvotes: 1
Reputation: 61
I am not sure I fully understood your question, taking a shot nevertheless.
Inside your while loop, after the first regex, store the email address in a variable.
e.g. my $addressList = $1;
and set a temp variable my $temp = 1;
.
Later you can do a if ($temp)
and if the next line is matching your regex you keep appending to the $addressList variable until the marker (q33BDrP9007220) is hit.
HTH
Upvotes: 0