我有“我爱Suzi和Marry”,我想把“Suzi”改成“Sara”。

firstString="I love Suzi and Marry"
secondString="Sara"

预期的结果:

firstString="I love Sara and Marry"

当前回答

如果字符串包含正则表达式字符,那么使用Bash比使用sed更好。

echo ${first_string/Suzi/$second_string}

它可以移植到Windows,并且至少可以与Bash 3.1兼容。

为了表明你不需要太担心逃跑,让我们把这个:

/home/name/foo/bar

到这个:

~/foo/bar

但前提是/home/name在开头。我们不需要sed!

假设Bash给了我们神奇的变量$PWD和$HOME,我们可以:

echo "${PWD/#$HOME/\~}"

感谢Mark Haferkamp在评论中提到引用/转义~.*

注意变量$HOME包含斜杠,但这并没有破坏任何东西。

进一步阅读:高级bash脚本编写指南。 如果必须使用sed,请确保转义每个字符。

其他回答

模式,将第一次出现替换为特殊的包机:

${参数/模式/字符串}

将所有发生的情况替换为特殊特许状的模式:

${参数/ /模式/字符串}

firstString="I love //Suzi// and Marry"
secondString="Sara"
firstString="${firstString/\/\/Suzi\/\//"$secondString"}"
echo $firstString

上面印着:我爱莎拉和玛丽

如果字符串包含正则表达式字符,那么使用Bash比使用sed更好。

echo ${first_string/Suzi/$second_string}

它可以移植到Windows,并且至少可以与Bash 3.1兼容。

为了表明你不需要太担心逃跑,让我们把这个:

/home/name/foo/bar

到这个:

~/foo/bar

但前提是/home/name在开头。我们不需要sed!

假设Bash给了我们神奇的变量$PWD和$HOME,我们可以:

echo "${PWD/#$HOME/\~}"

感谢Mark Haferkamp在评论中提到引用/转义~.*

注意变量$HOME包含斜杠,但这并没有破坏任何东西。

进一步阅读:高级bash脚本编写指南。 如果必须使用sed,请确保转义每个字符。

纯POSIX shell方法,与Roman Kazanovskyi基于sed的答案不同,它不需要外部工具,只需要shell自己的本地参数展开。注意,长文件名被最小化,所以代码更适合一行:

f="I love Suzi and Marry"
s=Sara
t=Suzi
[ "${f%$t*}" != "$f" ] && f="${f%$t*}$s${f#*$t}"
echo "$f"

输出:

I love Sara and Marry

工作原理:

Remove Smallest Suffix Pattern. "${f%$t*}" returns "I love" if the suffix $t "Suzi*" is in $f "I love Suzi and Marry". But if t=Zelda, then "${f%$t*}" deletes nothing, and returns the whole string "I love Suzi and Marry". This is used to test if $t is in $f with [ "${f%$t*}" != "$f" ] which will evaluate to true if the $f string contains "Suzi*" and false if not. If the test returns true, construct the desired string using Remove Smallest Suffix Pattern ${f%$t*} "I love" and Remove Smallest Prefix Pattern ${f#*$t} "and Marry", with the 2nd string $s "Sara" in between.

对于Dash,之前的所有帖子都不起作用

POSIX sh兼容的解决方案是:

result=$(echo "$firstString" | sed "s/Suzi/$secondString/")

这将替换每一行输入中的第一个出现的位置。添加/g标记替换所有出现的情况:

result=$(echo "$firstString" | sed "s/Suzi/$secondString/g")

我发现的唯一方法是将字符串存储在一个文件中,使用sed,然后将文件内容存储在一个var中:

echo "I love Suzy" > tmp.txt
sed -i "s/Suzy/Sarah/" tmp.txt
set res=`cat tmp.txt`
echo $res
rm tmp.txt

我不知道我正在使用哪种shell(如果我键入'sh',我唯一发现的是sh-4.2),但所有经典语法都失败了,比如简单的test=${test2}。 它失败了2次:在赋值处(必须使用set)和${}处。