用sed替换文本 [英] Replace text using sed

查看:134
本文介绍了用sed替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我收到上次修改日期如下:

  olddate =`grep -m1内置script.sh cut -c 22-29` 

我得到当前日期:

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

基本上我想用新的日期替换旧的日期

  sed -i s / $ olddate / $ newdate / gscript.sh 

但这不起作用日期包含斜杠。我周围环顾四周,找不到正确逃脱的方式。任何帮助将不胜感激。

解决方案

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

  sed -i; $ olddate; $ newdate; gscript.sh 


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.

解决方案

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

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

这篇关于用sed替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆