So it would read the folder paths into an array, check each line of the full file and replace the value with the reference value if found and write it back to the file.

That sounds sensible, as long as the list of folder paths isn't too big.

An important gotcha to consider: is any valid path a prefix of another valid path? If both "/foo" and "/foob" are valid, then "/foobar" could be either "/foo" + "bar" or "/foob" + "ar", and you will need some other heuristic to help decide between those interpretations. (A particularly likely case is that both "/foo" and "/foo/baz" are valid, but that this case can still be decided since the filename cannot contain "/".)

If neither of those are problems, your example code looks close to something that would work; however you probably need to remove the "\b" at either end of the match - you need to match 'CMD="/unix/path/to/folder/is/here' followed by 'file_name.sh"'. If you can include the additional "CMD=" prefix as part of the pattern, that would also help both to ensure it doesn't change anything it shouldn't and to let the regexp run faster: $regex = qr{ CMD=" (?: $references ) }x.

I would also recommend showing the input line in a warning if you have a CMD attribute but the regexp does not match it: if the program misses something, it's usually a lot easier to correct the program and run it again over the original file than to attempt multiple successive fixups.


In reply to Re: In place replacement from reference list by hv
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.