#!/usr/bin/perl use warnings; use strict; open(LOG, "source.txt") or die "Error: $!"; my @lines = ; close(LOG); foreach my $line (@lines) { $line =~ m/(\w)\s+(\d+),\s+(\d+)\s+(\(Birthday\)\s.+)/i; # 1 is month, 2 is day of month, 3 is year, 4 is the text print "$1 $2 $3 $4\n"; open(OUT, ">> $1/$2.txt") or die "Error"; # write our line appending to the bottom of the file print OUT "$3, $4\n"; } print "Done";