如何使用sed [duplicate]用“:”替换文本

时间:2022-01-18 08:29:43

This question already has an answer here:

这个问题在这里已有答案:

I am trying to replace a text with the help of sed but I am really puzzled about in finding the reason for failure.

我试图在sed的帮助下替换文本,但我真的很困惑在找到失败的原因。

    Main file
    ===========
    user@shell>cat test
    new=21:58:22
    old=09:58:22

    echo $new
    echo $old

    user@shell>sed -i 's/$old/$new/g' yoo
    user@shell>
    user@shell>
    desired output
    ==============
    user@shell>cat yoo
    09:58:22
    09:58:22
    09:58:2209:58:22
    09:58:2209:58:22
    asdasd
    user@shell>

After this I was assuming that I will get the new values in file yoo but no changes are reflection.

在此之后,我假设我将在文件yoo中获取新值,但没有更改是反射。

1 个解决方案

#1


0  

You need to expand the variables using double quotes like this,

你需要使用这样的双引号来扩展变量,

sed -i "s/$old/$new/g" yoo

#1


0  

You need to expand the variables using double quotes like this,

你需要使用这样的双引号来扩展变量,

sed -i "s/$old/$new/g" yoo