Last active 22 hours ago

dates2logs.pl Raw
1#!/usr/bin/env perl
2
3use 5.38.2;
4use strict;
5use warnings;
6
7use File::Basename;
8use File::Find;
9use File::Temp qw(tempfile);
10use File::Copy;
11
12# find all logs recursively
13my $dir = "/home/kat/.irc_history/logs";
14my @logs; # create log array, empty for now
15my @findlogs = find(\&wanted, $dir); # this adds everything to @logs i think
16
17sub wanted {
18 return unless -f;
19 push @logs, $File::Find::name; # add filenames with paths to @logs
20}
21
22foreach 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}