bash sed 替换日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31347753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:19:40  来源:igfitidea点击:

Sed replace date

linuxbashsed

提问by tadamhicks

So, I have looked over other questions and nothing specific seems like it can help me. I have a file that has a date variable set like mm/dd/yyyy that I would like to replace the date with.

所以,我查看了其他问题,似乎没有什么具体的问题可以帮助我。我有一个文件,它的日期变量设置为 mm/dd/yyyy,我想用它替换日期。

For example:

例如:

version.js

版本.js

//Application Version Information
app_date="7/07/2015";
...

And I would like to use sed to replace it. I actually have a shell script that replaces many things in this file, and all work except for the date. Currently what I'm trying is:

我想用 sed 来代替它。我实际上有一个 shell 脚本来替换这个文件中的很多东西,除了日期之外所有的东西都可以工作。目前我正在尝试的是:

sed -i -e 's:app_date="[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]":app_date="$(date %m/%d/%Y)":g'

But this isn't getting me the results I want. I've also tried:

但这并没有让我得到我想要的结果。我也试过:

sed -ibak 's/app_date=\"[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9][0-9][0-9]\"/app_date=$(date +%m/%d/$Y)\"/g'

Neither seem to work.

两者似乎都不起作用。

Any ideas?

有任何想法吗?

Edited to add:

编辑添加:

The solution I've successfully employed is Sean Bright's with double quotes:

我成功采用的解决方案是 Sean Bright 的双引号:

sed -i -e "s:app_date=".*";:app_date="$(date +%m/%d/%Y)";:g" version.js

This works perfectly for my needs.

这非常适合我的需要。

回答by Sean Bright

I bet your script would work if the date you were matching was October 10th...

我敢打赌,如果您匹配的日期是 10 月 10 日,您的脚本会起作用……

sed -i -e 's:app_date="[0-9][0-9]*/[0-9][0-9]*/[0-9][0-9][0-9][0-9]":app_date="$(date %m/%d/%Y)":g'

Add the *s after your day and month expressions as above. [0-9][0-9]will not match 7.

*在上面的日和月表达式后添加s。[0-9][0-9]不会匹配7

Now that I think about it, because you don't care about the date that is already in the file, most of this is unnecessary, you can simply do:

现在想想,因为你不关心文件中已经存在的日期,大部分都是不必要的,你可以简单地做:

sed -i -e 's:app_date=".*";:app_date="'$(date +%m/%d/%Y)'";:g' version.js

回答by geirha

You're using the wrong shell quotes. the command substitution $()will not be expanded inside single quotes.

您使用了错误的 shell 引号。命令替换$()不会在单引号内展开。

sed -i "/^app_date=/capp_date=\"$(date +%m/%d/%Y)\";" version.js # GNU only

Another approach, using a file editor instead of a stream editor:

另一种方法,使用文件编辑器而不是流编辑器:

ed -s version.js << EOF
/^app_date=/c
app_date="$(date +%m/%d/%Y)";
.
w
EOF

回答by keshav mainali

For replacing date with anything in Linux

用于在 Linux 中用任何内容替换日期

n=`date +%d`

cal>temp

If [$n -lt 10]   

//10 is just a predction for condition


then

Sed s/"$n"/*/g temp   

else

Sed s/"$n"/**/g temp

fi

// s is text substituting option //* is anything that replaces the date // g is global replacement

// s 是文本替换选项 //* 是任何替换日期的内容 // g 是全局替换