Chris F
Chris F

Reputation: 16673

sed command doesn't replace the pattern that I want?

I have an index.html file that has lines like

<script src="js/config.js"></script>
<link href="css/colorpicker.min.css" rel="stylesheet" />

IOW, it references a lot of *.js and *.css files. I want to replace all occurrences of *.js and *.css with *.css?rel=1569974280190463223 and *.js?rel=1569974280190463223 where 1569974280190463223 is a timestamp value, so the above lines will look like

<script src="js/config.js?rel=1569974280190463223"></script>
<link href="css/colorpicker.min.css?rel=1569974280190463223" rel="stylesheet" />

I have a shell script that does this

timestamp="$(($(date +%s%N)-T))"
sed -i -e 's|\.css"|\.css?rel=$timestamp"|g' -e 's|\.js"|\.js?rel=$timestamp"|g' index.html

but index.html file ends up looking like

<script src="js/config.js?rel=$timestamp"></script>
<link href="css/colorpicker.min.css?rel=$timestamp" rel="stylesheet" />

What am I missing in my sed line?

Upvotes: 0

Views: 30

Answers (1)

tshiono
tshiono

Reputation: 22012

You are quoting the shell variable $timestamp within single quotes. Please try instead:

sed -i -e 's|\.css"|\.css?rel='"$timestamp"'"|g' -e 's|\.js"|\.js?rel='"$timestamp"'"|g' index.html

Upvotes: 1

Related Questions