Sed replace date

12,388

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
Share:
12,388

Related videos on Youtube

tadamhicks
Author by

tadamhicks

Solutions Architect at honeycomb.io. Sharing the goodness of observability with the world!

Updated on June 04, 2022

Comments

  • tadamhicks
    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
      anubhava almost 9 years
      What is your expected output?
    • shellter
      shellter almost 9 years
      your 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 an ls listing. Good luck.
    • Etan Reisner
      Etan Reisner almost 9 years
      A 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
      tadamhicks almost 9 years
      I 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
      Ed Morton almost 9 years
      The 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 to sed -i -e 's:app_date=".*";:app_date="'$(date +%m/%d/%Y)'";:g' version.js like @SeanBright suggested.
    • tadamhicks
      tadamhicks almost 9 years
      Ed, 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
    Etan Reisner almost 9 years
    Do you have a reason to keep the doubled ranges instead of using [0-9]\+?
  • Etan Reisner
    Etan Reisner almost 9 years
    For sed? Yes. For sed -r? No. It is a regex dialect thing.
  • tadamhicks
    tadamhicks almost 9 years
    Sean, 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
    tadamhicks almost 9 years
    Thanks, I also found using double quotes to start the search-replace string in sed to work.