Du könntest ja einen Array mit den Ausgabedaten erstellen, und erst am Ende rausschreiben.
Könnte dann so aussehen:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/perl
use strict;
use warnings 'all';
# dateinamen
my $inputfile = '/var/log/mail.log';
my $outputfile = '/opt/lampp/htdocs/greylisted.txt';
# oeffne inputfile
open(my$fhin, '<', $inputfile) or die "$inputfile: $!";
# array für die ausgabe
my @output;
# lies zeile für zeile
while (my$line = <$fhin>)
{
if ($line =~ m/\bGreylisted for 300\b/)
{
# wenn der Suchstringt vorkommt,
# und "RCPT from" gefunden wird,
# wird das ausgabearray erweitert
if ( $line =~ m/RCPT from\s*(.*?):/ )
{ push(@output, substr( $line, 0, 15 ) .' : '. $1) }
}
}
# inputfile schliessen
close $fhin;
# ausgabearray bearbeiten
unshift(@output, $#output+2);
# oeffne outputfile
open(my$fhout, '>', $outputfile) or die "$outputfile: $!";
# daten rausschreiben
map { print $fhout "$_$/" } @output;
# outputfile schliessen
close $fhout;
MfG