gpt4 book ai didi

sed - 为什么 sed 删除示例中的字符?

转载 作者:行者123 更新时间:2023-12-04 08:56:21 24 4
gpt4 key购买 nike

a=a
b=ab
c=''
d=''
echo ac | sed "s/$a/$b/;"
abc
echo ac | sed "s/$a/$b/; s/$c/$d/"
bc
为什么 sed 删除了最后一个例子中的字符“a”?

最佳答案

这是因为 $c是一个空字符串,当 sed通过后续s带有空模式的命令,使用前一个命令,在本例中为 $a , 即 a .
所以,

echo ac | \ 
sed "s/$a/$b/; s/$c/$d/"
# a ab a
这里,
  • a替换为 ab首先与 s/a/ab/ ( ac 变成 abc )
  • as/a// 删除( abc 变成了 bc )。

  • 参见文档, Regular Expressions in sed section :

    If an RE is empty (that is, no pattern is specified) sed shall behave as if the last RE used in the last command applied (either as an address or as part of a substitute command) was specified.

    关于sed - 为什么 sed 删除示例中的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63813864/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com