0

Hi I am trying to use a date variable in my sed command.

Can anyone see where I am going wrong?

sed -i -e '1,/<pubDate>"$(LC_ALL=nn_NO.UTF-8 date -d "2 days ago" +'%a, %d %b %Y')/!d"' file

I am trying to remove everything 2 days or more old from a rss dump

Thanks, Chris

Chris
  • 985
  • 2
  • 7
  • 17

1 Answers1

1

Command expansion isn't done inside single quotes (double quotes inside single quotes don't change this). You need to use double quotes around the whole command, not just around the command expansion.

sed -i -e "1,/<pubDate>$(LC_ALL=nn_NO.UTF-8 date -d "2 days ago" +'%a, %d %b %Y')/!d" file

But if you're doing this interactively you'll need to put the ! inside single quotes to prevent it from doing history expansion.

sed -i -e "1,/<pubDate>$(LC_ALL=nn_NO.UTF-8 date -d "2 days ago" +'%a, %d %b %Y')/"'!d' file

or turn off history expansion:

set +H
sed -i -e "1,/<pubDate>$(LC_ALL=nn_NO.UTF-8 date -d "2 days ago" +'%a, %d %b %Y')/!d" file
set -H

See How to escape history expansion exclamation mark ! inside a double quoted " command substitution like "$(echo '!b')"?

This isn't necessary if you're doing it in a script.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Unfortuantly that doesnt seem to work `chris@DESKTOP-3QUNCRU:~$ sed -i -e "1,/$(LC_ALL=nn_NO.UTF-8 date -d "2 days ago" +'%a, %d %b %Y')/\!d" test.rss sed: -e expression #1, char 30: unknown command: `\'` – Chris Jan 28 '19 at 18:14
  • Sorry, can't use backslash to escape the history character, I showed correct ways to do it. – Barmar Jan 28 '19 at 18:21