while ( ) { s{ (?:^\ +|\ +$) |(?:\:000[A,P]M) |(?:99991231) |(?:Jan\ 1\ 1900\ 12:00:00) }{}gx; s/[ ]*\~\t\~[ ]*/~/g; print OUT_FILE $_; } close IN_FILE; ## and unlink() the filename for IN_FILE ## then rename() outfile to infile.