There is an easy way in perl to do such edits in-place, but that's not what you want. Here's how.
We'll start with the splatline and other preliminaries. The splatline can be useful even on win32 systems, since perl can still obtain options from it.
We'll define a glob pattern for the input files, and an output path, and bring in a standard module function for extracting filenames from paths.#!/usr/bin/perl use warnings; use strict;
Now let's define a hash whose keys are the text to be replaced, and values, their replacement. We'll construct a big alternation regex from the keys. It will capture whatever it matches.my $infiles = '/path/to/RETAKC*.txt'; my $outpath = '/another/path/'; use File::Basename 'basename';
Now we're ready to do the deed. We get all our filenames with glob and loop over them. We open each to read, and an output file to accept the results. We apply our substitution globally to each line and print to the output file.my %substitute = ( '0000J' => '000-1', # . . . ); my $regex = qr/(${\join '|', keys %substitute})/;
That's it. All your substitutions made to new copies of all your files.for my $file (glob $infiles) { open my $in, '<', $file or warn $! and next; open my $out, '>', $outpath.basename($file) or warn $! and next; while (<$in>) { s/$regex/$substitute{$1}/g; print $out $_; } }
After Compline,
Zaxo
In reply to Re: Easy find and replace loop. HELP!
by Zaxo
in thread Find and replace by five character string
by wrml
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |