“Bash Sed с переменной” Ответ

SED с переменной

sed -i "s/$var1/ZZ/g" "$file"
Devops Captain

Bash Sed с переменной

# If you want to store your regex in a bash variable 
# and use it in sed, you might need the following
BASE_REGEX="[0-9]{4}\.[0-9]+"
REGEX_ESCAPED=$(sed 's/[}{)(+]/\\&/g; s/\^/\\^/g' <<< "$BASE_REGEX")

# One variable can be used with sed
sed -i "s/${REGEX_ESCAPED}/REPLACEMENT/g" file.txt
# The other can be used with bash internal regex operator
if [[ "2022.42" =~ ^${BASE_REGEX}$ ]]
then
	echo 'Matches !'
fi
Shy Shrew

Ответы похожие на “Bash Sed с переменной”

Вопросы похожие на “Bash Sed с переменной”

Больше похожих ответов на “Bash Sed с переменной” по Shell/Bash

Смотреть популярные ответы по языку

Смотреть другие языки программирования