Replace text using sed

2019-03-02 00:19发布

i am having trouble replacing the modified date in my script via sed.

I am getting the last modified date like this:

olddate=`grep -m1 "Built " script.sh | cut -c 22-29`

I get the current date with:

newdate=`date +%d/%m/%y`

Basically i want to replace old date with new date

sed -i "" "s/$olddate/$newdate/g" script.sh

But this doesn't work as the date contains slashes. I've looked around and i can't find the way to escape them properly. Any help would be appreciated.

3条回答
Bombasti
2楼-- · 2019-03-02 00:38

You can use separators other than slashes, for instance ";"

sed -i "" "s;$olddate;$newdate;g" script.sh
查看更多
看我几分像从前
3楼-- · 2019-03-02 00:48

use sed "s#$olddate#$newdate#g"

that should work

查看更多
狗以群分
4楼-- · 2019-03-02 00:55

Use , instead of / !

sed -i "" "s,$olddate,$newdate,g" script.sh

In fact you can use almost any char as separators.

查看更多
登录 后发表回答