如何用具有复杂字符的文本替换文件中的文本?

How to replace text in files with a text that has complicated characaters?

我需要运行下面的操作但是bash或者zsh终端不喜欢那些字符。我如何 运行 正确?

find ./ -type f -exec sed -i 's/PHRASE=TEST/pwds=`gpg --decrypt ~/test.gpg` & eval "$pwds"/gI' {} \;

PHRASE=TEST需要换成pwds=gpg --decrypt ~/test.gpg& eval "$pwds"

我不需要使用 sed,我可以使用其他东西,只要它有效

没有示例输入和预期输出,我无法 100% 验证,但我很确定您只需要转义文件路径中的 /&:

find ./ -type f -exec sed -i 's/PHRASE=TEST/pwds=`gpg --decrypt ~\/test.gpg` \& eval "$pwds"/gI' {} \;

或者,如果您愿意,可以通过更改 s 命令的定界符来避免第一次转义:

find ./ -type f -exec sed -i 's|PHRASE=TEST|pwds=`gpg --decrypt ~/test.gpg` \& eval "$pwds"|gI' {} \;

示例:

$ cat file.txt
line 1
line 2 PHRASE=TEST
line 3

$ find ./ -type f -exec gsed -i 's/PHRASE=TEST/pwds=`gpg --decrypt ~\/test.gpg` \& eval "$pwds"/gI' {} \;

$ cat file.txt
line 1
line 2 pwds=`gpg --decrypt ~/test.gpg` & eval "$pwds"
line 3