http://www.perlmonks.org?node_id=455183


in reply to Multiple substitutions in large files

#!/usr/bin/perl -pi s/^\|/\\N\|/; s/\|\s*$/\|\\N/; s/\|\s*\|/\|\\N\|/g; s/\|\.\s*\|/\|\\N\|/g; s/\|\s+/\|/g; s/\s+\|/\|/g; s/(\d{2}:\d{2}:\d{2})\.\d+/$1/g; s/(\d{5})-(?:\d{1,4}|\s+)/$1/;

Should be very much more effective, because it creates new files instead of trying to move all the bytes in the file around for each substitution.

See perlrun's info on the -i and -p switches.