use strict; use warnings; use POSIX qw(strftime); # Open the file for read and write open (my $fh, '+<', 'data.dat') or die "Unable to open data.dat: $!"; # Specified: day/month/yyyy h:m:s am or pm # assumed dd/mm/yyyy hh:mm:ss am/pm my $new_stamp = strftime ('%d/%m/%Y %I:%M:%S %p',localtime); # This gets AM/PM insteam of am/pm, so: $new_stamp =~ s/([PA]M)$/lc $1/e; while (<$fh>) { # Replace date/time stamp with new one if (s|\d\d/\d\d/\d\d\d\d \d\d:\d\d:\d\d [ap]m|$new_stamp|) { seek ($fh,-length($_)-1, 1); print $fh $_; } } close ($fh)