4

How can I do a string replace in bash using multiple lines pattern?

To illustrate I present a pseudocode:

TARGET_STR=' $N = "magic_quotes_gpc = <b>"._("On")."</b>"; $D = _("Increase your server security by setting magic_quotes_gpc to 'on'. PHP will escape all quotes in strings in this case."); $S = _("Search for 'magic_quotes_gpc' in your php.ini and set it to 'On'."); $R = ini_get('magic_quotes_gpc'); $M = TRUE; $this->config_checks[] = array("NAME" => $N , "DESC" => $D , "RESULT" => $R , "SOLUTION" => $S , "MUST" => $M );' REPLACE_STR=' /* NOTE: "Magic_quotes_gpc" is no longer required. We taught GOsa2 to deal with it (see /usr/share/gosa/html/main.php). By Questor */ /* Automatic quoting must be turned on */ /* $D = _("Increase your server security by setting magic_quotes_gpc to 'on'. PHP will escape all quotes in strings in this case."); $S = _("Search for 'magic_quotes_gpc' in your php.ini and set it to 'On'."); $R = ini_get('magic_quotes_gpc'); $M = TRUE; $this->config_checks[] = array("NAME" => $N , "DESC" => $D , "RESULT" => $R , "SOLUTION" => $S , "MUST" => $M ); */' PATH_TO_FILE='/usr/share/gosa/setup/class_setupStep_Checks.inc' some_command '$TARGET_STR/$REPLACE_STR' $PATH_TO_FILE 

NOTE I: I would like to use bash variables.
NOTE II: I would like to use the strings without escape (strings in TARGET_STR and REPLACE_STR).

Thanks!

    1 Answer 1

    7

    You could convert all of the relevant text strings to hexadecimal strings, perform the replacement in hexadecimal, and then convert the result back to text. Here is what that might look like:

    #!/bin/bash # replace.sh # Set the target string, i.e. the string to replace read -r -d '' TARGET_STR <<'HEREDOC' $N = "magic_quotes_gpc = <b>"._("On")."</b>"; $D = _("Increase your server security by setting magic_quotes_gpc to 'on'. PHP will escape all quotes in strings in this case."); $S = _("Search for 'magic_quotes_gpc' in your php.ini and set it to 'On'."); $R = ini_get('magic_quotes_gpc'); $M = TRUE; $this->config_checks[] = array("NAME" => $N , "DESC" => $D , "RESULT" => $R , "SOLUTION" => $S , "MUST" => $M ); HEREDOC # Set the replacement string, i.e. the string to replace the target string with read -r -d '' REPLACE_STR <<'HEREDOC' /* NOTE: "Magic_quotes_gpc" is no longer required. We taught GOsa2 to deal with it (see /usr/share/gosa/html/main.php). By Questor */ /* Automatic quoting must be turned on */ /* $D = _("Increase your server security by setting magic_quotes_gpc to 'on'. PHP will escape all quotes in strings in this case."); $S = _("Search for 'magic_quotes_gpc' in your php.ini and set it to 'On'."); $R = ini_get('magic_quotes_gpc'); $M = TRUE; $this->config_checks[] = array("NAME" => $N , "DESC" => $D , "RESULT" => $R , "SOLUTION" => $S , "MUST" => $M ); */' HEREDOC # Set the path to the input file PATH_TO_FILE="target-string.txt" # Set file paths PATH_TO_HEX_FILE="${PATH_TO_FILE}.hex" PATH_TO_REPLACED_FILE="replaced-${PATH_TO_FILE}" # Convert the two strings to hexadecimal TARGET_STR_HEX="$(echo "${TARGET_STR}" | xxd -p | tr -d '\n')" REPLACE_STR_HEX="$(echo "${REPLACE_STR}" | xxd -p | tr -d '\n')" # Conver the input file to hexadecimal xxd -p "${PATH_TO_FILE}" | tr -d '\n' > "${PATH_TO_HEX_FILE}" # Perform the replacement using hexadecimal strings sed -i'' "s/${TARGET_STR_HEX}/${REPLACE_STR_HEX}/g" "${PATH_TO_HEX_FILE}" # Convert the result back to text xxd -r -p "${PATH_TO_HEX_FILE}" "${PATH_TO_REPLACED_FILE}" # Remove intermediate file rm "${PATH_TO_HEX_FILE}" 

    This produces the desired result for the test data your provided.

    3
    • I've tried your script. See the output: <pre>bash replace.bash replace.bash: line 30: class_setupStep_Checks.inc: syntax error: invalid arithmetic operator (error token is ".inc") sed: -e expression #1, char 1: unknown command: `.' replace.bash: line 51: class_setupStep_Checks.inc.hex: syntax error: invalid arithmetic operator (error token is ".inc.hex")</pre>. Thanks a lot! =DCommentedMay 24, 2018 at 16:20
    • 1
      @EduardoLucio That error had to do with different versions of wc producing slightly different output. I rewrote the example script. Try it now and let me know if it works.
      – igal
      CommentedMay 24, 2018 at 16:40
    • Man... You're awesome! Work like a charm! This is a problem I've been trying to solve for quite some time. And so far no response was effective as yours! Thanks! =DCommentedMay 24, 2018 at 17:28

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.