Binary patch using perl

for all who are interested:

  • a bash function scanning all unique strings in FILE that contain a token and replacing it.
  • Furthermore the function demonstrates how to construct a perl command from variables in a script:
 patchfile() {
        local -r file="$1"
        local -r tok="$2"
        local -r replaceby="$3"
        local rc=0
...
        # Find all unique strings in FILE that contain the pattern
        perl -pi -wE 's/'$tok'/'$replaceby'/g' $file
        rc=$?
...
        return $rc
    }

Note that with the quoting you're using on the perl command you're constructing, you won't get the desired results if any of $file , $tok , and $replaceby expand to strings that contain any <space>, <tab>, or <newline> characters. A safer and shorter way to write that line would be:

        perl -pi -wE "s/$tok/$replaceby/g" "$file"