20,000 lines is not likely to be more than 2M or so. That is not so big as to prevent slurping on most machines. The 40-some phrases in the junk file are insignificant in size, so lets compile them just once.
That should give decent performance. I've locked the file after opening to read and write, so you are protected from races.use Fcntl ':flock'; my @regexen; { open my $rx, '<', '/path/to/junk.txt' or die $!; @regexen = map { chomp; qr/\Q$_\E/ } <$rx>; close $rx or die $!; } for (@list_of_files) { local $/; open my $fh, '+<', $_ or warn $! and next; flock $fh, LOCK_EX; my $contents = <$fh>; # study $contents; # may want to try this $contents =~ s/$_//g for @regexen; seek $fh, 0, 0; print $fh $contents or warn $! and next; close $fh or warn $! and next; }
Update: MidLifeXis' reply reminded me to chomp before compiling the regexen. Added to code, along with revised error handling in the big loop.
After Compline,
Zaxo
In reply to Re: file substitution
by Zaxo
in thread file substitution
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |