ACCOUNT,DATE,NOTE H123456,20151209,THIS IS A TEST #### use strict; use warnings; my %reformat; my $re = qr{^(H\d+,\d+,)(.*)$}; open (NEW, ">", "Notes_Test_OUTPUT.txt" ) or die "could not open:$!"; open (FILE, "<", "Notes_Test.txt") or die "could not open:$!"; while () { chomp; /$re/; push @{ $reformat{$1} }, $2; } print NEW if $_, join ' ', @{ $reformat{$_} } for keys %reformat; close (FILE); close (NEW);