1

I want to have the value of

date + "%Y%m%d"

to be piped as the substituting word in sed

cat template.html | sed s/\\@\\@DATENUMBER\\@\\@/date +"%Y%m%d"/g

But this isn't allowed. How do I extract the value of the date and pipe it correctly?

byInduction
  • 405
  • 1
  • 6
  • 13

1 Answers1

3

Try:

sed "s/@@DATENUMBER@@/$(date +"%Y%m%d")/g" template.html

Notes:

  1. There is no need for cat because sed will take a file name as an argument.

  2. If the text that you want to replace looks like @@DATENUMBER@@, then there is no need for all those backslashes: @ is neither a shell-active character nor a sed-active character and does not need to be escaped.

  3. If you want the date command to be executed, you have to use command substitution, $(...).

  4. The whole of the sed s command should be in quotes so that the shell does not do word splitting on it.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • Thanks! What does "word splitting" mean in this context? – byInduction May 16 '16 at 23:42
  • Your welcome. For the original command, `sed` does not see a complete substitution command. Instead it sees `s/\\@\\@DATENUMBER\\@\\@/date` as its first argument and `+"%Y%m%d"/g` as its second argument. Any time you have something that contains a space and you want the command to see it as a single argument, it needs to be in quotes. – John1024 May 16 '16 at 23:47
  • For more on word splitting, see for example [this answer](http://stackoverflow.com/a/18498494/3030305). For an advanced discussion, see [this FAQ](http://mywiki.wooledge.org/WordSplitting). – John1024 May 17 '16 at 00:06
  • Actually, in this particular case, word splitting will not result in multiple arguments - `sed s/@@DATENUMBER@@/$(date +%Y%m%d)/g template.html` is equivalent - though not recommended. This works because the `date` command substitution will result in just digits, leaving no IFS in the argument. I still recommend using the quotes, even if you're really sure what you're doing; the next person to read it might not be so sure, which is really annoying when that person is Future You... – Toby Speight May 17 '16 at 08:08