0
Follow
0
View

sed replacing string with special characters

cxzhou520 注册会员
2023-01-24 22:58

Following script should work for you:

key="12'{}34[];|^)(*&^!^#~\`!-567"
escappedKey=$(printf '%s\n' "$key" | sed 's/[]\/$*.^[]/\\&/g');

value="345$\`{}[]|%';"
escappedValue=$(printf '%s\n' "$value" | sed 's/[]\/$*.^[]/\\&/g');

sed "s/$escappedKey/$escappedValue/g" hello.txt

Note that you will need to escape tilde as \' in double quotes and also you are using $key to populate escappedValue by mistake.

ABC="345$`{}[]|%';"

About the Author

Question Info

Publish Time
2023-01-24 22:58
Update Time
2023-01-24 22:58