0

So I know that in order to replace something with the current date it would be

sed "s/something/`date +"%m-%d-%Y"`/" file.txt

This would give me the date in mm-dd-YYYY But I want to be able the get the date in mm/dd/YYYY

  • What do you mean by proper format? What is the exact command you are running and what is the expected output supposed to look like? Please consider editing your post to include more context. Thank you! – kemotep Oct 9 at 19:24
  • 1
    Yes, please clarify. Do you mean m/d/y as you have in the question title? Shouldn't that be mm/dd/yy? Or perhaps mm/dd/yyyy? Please edit your question and add an example showing a specific date. – terdon Oct 9 at 19:31
2

'/' has by default a special meaning for sed, so you need to either escape the '/' like this:

sed "s/something/`date +"%m\/%d\/%Y"`/" file.txt

or use another separator for sed, like this and there is no need to escape the '/':

sed "s|something|`date +"%m/%d/%Y"`|" file.txt
  • I'd still recommend you use the more modern $( ... ) construct instead of backticks. – roaima Oct 9 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.