Chris
Chris

Reputation: 1219

Using date variable in sed

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

Upvotes: 0

Views: 193

Answers (1)

Barmar
Barmar

Reputation: 781068

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.

Upvotes: 1

Related Questions