Bash

使用多行模式在bash中替換字元串

  • May 24, 2018

如何使用多行模式在 bash 中進行字元串替換?

為了說明,我提供了一個虛擬碼:

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

**注意 I:**我想使用 bash 變數。

**註二:**我想使用沒有轉義的字元串(TARGET_STR 和 REPLACE_STR 中的字元串)。

謝謝!

您可以將所有相關的文本字元串轉換為十六進製字元串,以十六進制執行替換,然後將結果轉換回文本。這可能是這樣的:

#!/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}"

這將為您提供的測試數據產生所需的結果。

引用自:https://unix.stackexchange.com/questions/445651