Sed replace date
Solution 1
I bet your script would work if the date you were matching was October 10th...
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
.
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
Solution 2
For replacing date with anything in 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
Solution 3
You're using the wrong shell quotes. the command substitution $()
will not be expanded inside single quotes.
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
Related videos on Youtube
tadamhicks
Solutions Architect at honeycomb.io. Sharing the goodness of observability with the world!
Updated on June 04, 2022Comments
-
tadamhicks almost 2 years
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.
For example:
version.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 -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:
sed -i -e "s:app_date=".*";:app_date="$(date +%m/%d/%Y)";:g" version.js
This works perfectly for my needs.
-
anubhava almost 9 yearsWhat is your expected output?
-
shellter almost 9 yearsyour sample "day" value
7/
won't match your regex of[0-9][0-9]/
Also use YYYY-MM-DD format for file dates, and they will sort automatically in anls
listing. Good luck. -
Etan Reisner almost 9 yearsA general rule of regular expressions is "don't over-match". So see the updated second part of Sean Bright's answer for how to avoid matching things you don't care about.
-
tadamhicks almost 9 yearsI want the expected output to be today's date. So, if I embed the sed command in a script and run the script I want it to change the date to today.
-
Ed Morton almost 9 yearsThe solution you've successfully employed is using incorrect quoting, I'm actually not sure why it's even running without giving you a syntax error. You want all of your sed script inside single quotes to protect it from shell expansion but right now you have most of it in double quotes and actually escape from that into unquoted text for
.*
. Change it back tosed -i -e 's:app_date=".*";:app_date="'$(date +%m/%d/%Y)'";:g' version.js
like @SeanBright suggested. -
tadamhicks almost 9 yearsEd, the double quotes around .* are actually part of the string. I found the use of double quotes for the sed command here: unix.com/shell-programming-and-scripting/… It works because it is syntactically correct. Either way works, in fact. The reason I'm sticking with double quotes is that the original script author did in other places, too. Keeps things standard for those that inherit. I suppose I could sed to fix that, though.
-
-
Etan Reisner almost 9 yearsDo you have a reason to keep the doubled ranges instead of using
[0-9]\+
? -
Etan Reisner almost 9 yearsFor
sed
? Yes. Forsed -r
? No. It is a regex dialect thing. -
tadamhicks almost 9 yearsSean, this is awesome. However, it now replaces the date with "$(date %m/%d/%Y)" instead of converting this to today's date. Do I need to escape the quotes and then invoke the date?
-
tadamhicks almost 9 yearsThanks, I also found using double quotes to start the search-replace string in sed to work.