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