dates2logs.pl
· 1.2 KiB · Perl
Raw
#!/usr/bin/env perl
use 5.38.2;
use strict;
use warnings;
use File::Basename;
use File::Find;
use File::Temp qw(tempfile);
use File::Copy;
# find all logs recursively
my $dir = "/home/kat/.irc_history/logs";
my @logs; # create log array, empty for now
my @findlogs = find(\&wanted, $dir); # this adds everything to @logs i think
sub wanted {
return unless -f;
push @logs, $File::Find::name; # add filenames with paths to @logs
}
foreach my $log (@logs) {
my ($fn, $dir, $ext) = fileparse($log, ".log");
my ($outfh, $outfn) = tempfile();
open(my $fh, "<", "$log") or die("could not open $log: $!");
while (my $line = <$fh>) {
# BRENNAN HELPED HERE TY BRENNAN
# Only match lines with a bare time like "[12:55:02]"
# the old format missing a date.
# e.g. "[2026-01-01 12:55:02]", won't match
# this because right after "["
# it sees "20", not two digits followed by a ":".
if ($line =~ /^\[\d{2}:\d{2}:\d{2}\]/) {
my $newts = substr($line, 1);
my $newdts = "[$fn $newts";
print $outfh $newdts;
} else {
# do nothing just print what's already there
print $outfh $line;
};
}
close $outfh;
copy($outfn, $log)
}
| 1 | #!/usr/bin/env perl |
| 2 | |
| 3 | use 5.38.2; |
| 4 | use strict; |
| 5 | use warnings; |
| 6 | |
| 7 | use File::Basename; |
| 8 | use File::Find; |
| 9 | use File::Temp qw(tempfile); |
| 10 | use File::Copy; |
| 11 | |
| 12 | # find all logs recursively |
| 13 | my $dir = "/home/kat/.irc_history/logs"; |
| 14 | my @logs; # create log array, empty for now |
| 15 | my @findlogs = find(\&wanted, $dir); # this adds everything to @logs i think |
| 16 | |
| 17 | sub wanted { |
| 18 | return unless -f; |
| 19 | push @logs, $File::Find::name; # add filenames with paths to @logs |
| 20 | } |
| 21 | |
| 22 | foreach my $log (@logs) { |
| 23 | my ($fn, $dir, $ext) = fileparse($log, ".log"); |
| 24 | |
| 25 | my ($outfh, $outfn) = tempfile(); |
| 26 | |
| 27 | open(my $fh, "<", "$log") or die("could not open $log: $!"); |
| 28 | while (my $line = <$fh>) { |
| 29 | # BRENNAN HELPED HERE TY BRENNAN |
| 30 | # Only match lines with a bare time like "[12:55:02]" |
| 31 | # the old format missing a date. |
| 32 | # e.g. "[2026-01-01 12:55:02]", won't match |
| 33 | # this because right after "[" |
| 34 | # it sees "20", not two digits followed by a ":". |
| 35 | if ($line =~ /^\[\d{2}:\d{2}:\d{2}\]/) { |
| 36 | my $newts = substr($line, 1); |
| 37 | my $newdts = "[$fn $newts"; |
| 38 | print $outfh $newdts; |
| 39 | } else { |
| 40 | # do nothing just print what's already there |
| 41 | print $outfh $line; |
| 42 | }; |
| 43 | } |
| 44 | close $outfh; |
| 45 | copy($outfn, $log) |
| 46 | } |