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


In reply to Re: Easy find and replace loop. HELP! by Zaxo
in thread Find and replace by five character string by wrml

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.