in reply to Find and replace by five character string

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.

#!/usr/bin/perl use warnings; use strict;
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.
my $infiles = '/path/to/RETAKC*.txt'; my $outpath = '/another/path/'; use File::Basename 'basename';
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 %substitute = ( '0000J' => '000-1', # . . . ); my $regex = qr/(${\join '|', keys %substitute})/;
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.
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 $_; } }
That's it. All your substitutions made to new copies of all your files.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Easy find and replace loop. HELP!
by Anonymous Monk on Aug 12, 2005 at 05:44 UTC
    Thanks you very much for the help. I'm interested to learn why this works. I think I understand everything except how the module works and what is happening here:

    my $regex = qr/(${\join '|', keys %substitute})/;

    If you would be so kind as to explicate I would be oh so grateful.

    walter

      The File::Basename module is a library which just defines a number of functions which are useful for extracting pieces of file paths. By giving 'basename' as an argument to the use statement which loads the module, we get the basename() function imported to our main:: namespace. That lets us call it without its fully qualified name, File::Basename::basename, in constructing the output file path. The module is standard, it has shipped with perl for years.

      The regex construction is somewhat more compact than you should usually expect to see. The qr// operator is a quote which produces a compiled regex out of its contents. It interpolates variables (things starting with $ or @) in the same way double quotes or qq// do. Instead of producing a named variable with the regex text in it, I used dereference (${}) of a reference to the regex text to get an "anonymous variable" to be interpolated. All that because interpolation in quotes does not call functions, but just fills in things with $ or @ sigils. That line could have been split in two in a way that many would prefer,

      my $alt = join '|', keys %substitute; # make one string of all # keys with pipes between my $regex = qr/($alt)/; # compile the regex # with parens to capture
      but I prefer having the assignment made in a single statement without superfluous variables.

      After Compline,
      Zaxo