简体   繁体   English

使用sed替换文本

[英]Replace text using sed

i am having trouble replacing the modified date in my script via sed. 我无法通过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. 任何帮助,将不胜感激。

您可以使用除斜杠以外的其他分隔符,例如“ ;

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

Use , instead of / ! 使用代替/!

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

In fact you can use almost any char as separators. 实际上,您几乎可以使用任何char作为分隔符。

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

that should work 应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM