Here's another example based on hv's suggestion:

Win8 Strawberry 5.30.3.1 (64) Tue 09/06/2022 17:27:14 C:\@Work\Perl\monks >perl use 5.010; # need \K regex extension use strict; use warnings; use autodie; use Data::Dump qw(dd); # for debug my $file_paths = <<'EOFILEPATHS'; # known file paths /unix/path/to/folder/is/here /unix/path/to/folder/also /unix/path/to/another/folder EOFILEPATHS open my $fh_reference_paths, '<', \$file_paths; my ($rx_reference) = map qr{ $_ }xms, join ' | ', map quotemeta, reverse sort map { chomp; $_; } <$fh_reference_paths> ; # dd '$rx_reference', $rx_reference; # for debug close $fh_reference_paths; my $b0rken = <<'EOBORKEN'; # paths with appended files, some borked CMD="/unix/path/to/folder/is/herefile_name.sh" #CMD="/unix/path/to/folder/is/herefile_name.sh" XYZ="/unix/path/to/folder/is/herefile_name.sh" XYZ="/unix/path/to/folder/is/here/file_name.ok" CMD = "/unix/path/to/another/folder/some_file.ok" CMD="/unix/path/to/folder/is/here/file_name.ok" CMD = "/unix/path/to/folder/alsosome_file.xy" EOBORKEN open my $fh_b0rken, '<', \$b0rken; my $add = '//'; # make addition evident for development BORKEN: while (<$fh_b0rken>) { $_ =~ s{ \A \s* CMD \s* = \s* " $rx_reference (?! /) \K }{$add}xms +; print $_; } close $fh_b0rken; ^Z CMD="/unix/path/to/folder/is/here//file_name.sh" #CMD="/unix/path/to/folder/is/herefile_name.sh" XYZ="/unix/path/to/folder/is/herefile_name.sh" XYZ="/unix/path/to/folder/is/here/file_name.ok" CMD = "/unix/path/to/another/folder/some_file.ok" CMD="/unix/path/to/folder/is/here/file_name.ok" CMD = "/unix/path/to/folder/also//some_file.xy"
This still has the problem noted by hv of mishandling ambiguous path-filenames. Given paths
    /unix/path/foo
    /unix/path/foob
how does one properly "correct" /unix/path/foobar.sh? The regex is designed to match the longest path (/unix/path/foob) in this case. See Building Regex Alternations Dynamically. (Of course, this problem goes away completely if one also knows all possible filenames that might be involved.)

Update: One approach to the ambiguous path-filename problem would be to capture all matches of the $rx_reference regex and report (warn or die) if there's more than one possibility. (If there's just one possibility, just fix it.) (This code has an interaction between regex (?{ CODE }) and a lexical variable, so it must use Perl version 5.18+. If your Perl is earlier, let me know and I can supply a simple fix. I'll just put the earlier version in as a comment. Also, use (?!) vice (*FAIL) pre-5.10.)

my @ambiguous; BORKEN: while (<$fh_b0rken>) { my @b0rken; # can use with perl version 5.18+ # local our @b0rken; use re 'eval'; # pre-5.18 version $_ =~ m{ \A (\s* CMD \s* = \s* " $rx_reference) ((?! /) [^\n]*) \n? \z (?{ push @b0rken, [ $1, $2 ] }) (*FAIL) # (?!) pre-5.10 }xms; if (@b0rken > 1) { # more than 1 possible breakage: report ambigu +ity chomp; push @ambiguous, join "\n", "'$_' ambiguous:", map " '$_->[0]'?'$_->[1]'", @b0rken; } elsif (@b0rken == 1) { # just 1 breakage: fix and output print "$b0rken[0]->[0]$add$b0rken[0]->[1]\n"; } else { # path looks ok: just output print; } } # end while BORKEN ... warn $_ for @ambiguous; # report ambiguities


Give a man a fish:  <%-{-{-{-<


In reply to Re: In place replacement from reference list (updated) by AnomalousMonk
in thread In place replacement from reference list by Misstre

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.