in reply to In place replacement from reference list
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: In place replacement from reference list
by Polyglot (Chaplain) on Sep 07, 2022 at 05:44 UTC |