#!/usr/bin/perl
use strict;
use warnings;
open (FILE,"noname.txt")|| die "file does not exist";
my @results;
while (<FILE>) {
if ($_ =~ m/:EOT/) {
# end of a record, print and reset
print join("#", @results);
@results = ();
print "\n";
} else {
chomp($_);
next if ($_ =~ m/^BOT:/); # skip start of record marker
if ($_ =~ m%[:/]%) {
$_ =~ s%\s+%#%; # replace spaces in datestamp with #
}
$_ =~ s%[/:]%%g; # strip / and :
$_ =~ s%^\s+%%; # strip leading whitespace
$_ =~ s%\s+$%%; # strip trailing whitespace
$_ =~ s%"%%g; # strip quotes
$_ =~ s%.*\s+=\s+(.*)%$1%g; # strip everything before the data
push @results, $_;
}
}
close (FILE);
Edited to note: I just noticed that the OP wants to have the data string "340806@osiris.fr.ft" transposed in the output, as "osiris.fr.ft#340806". That makes the above incorrect, and provides a more interesting challenge.
---------- Post updated at 08:53 AM ---------- Previous update was at 07:50 AM ----------
OK, here's another pass that handles the field with the @ correctly:
#!/usr/bin/perl
use strict;
use warnings;
open(FILE, "noname.txt") || die "Can't open file: $!\n";
while(<FILE>) {
chomp($_);
next if $_ =~ m/BOT:/;
if ($_ =~ m/:EOT/) {
print "\n";
} else {
#handle the time/date stamp
if ($_ =~ s%^(\d{4})/(\d{2})/(\d{2})\s+(\d{2}):(\d{2}):(\d{2})\s+%$1$2$3#$4$5$6%) {
print "$_";
next;
}
# handle the transposition of what looks like an email address
if ($_ =~ s%.*=\s+"(.*)@(.*)"\s+%$2#$1%) {
print "#$_";
next;
}
# handle everything else
# assumes a pretty standard format of <whitespace>ID<whitespace>=<data><maybe whitespace>
$_ =~ s/.*=\s+(.*)\s*/$1/; # pull out the data after the =
$_ =~ s/"//g; # strip quotes
$_ =~ s/^\s+//g; # strip leading spaces
$_ =~ s/\s+$//g; # strip trailing spaces
print "#$_";
}
}
close(FILE);