I want to take sitemap.xml
file and replace lastmod
with new timestamp.
Exaple of sitemap.xml:
<?xml version="1.0" encoding="UTF-8"?>
<urlset>
<url>
<lastmod>2020-08-02T07:30:53+00:00</lastmod>
<priority>1.00</priority>
</url>
<url>
<lastmod>2020-08-02T07:30:53+00:00</lastmod>
<priority>0.80</priority>
</url>
</urlset>
And my code:
field=lastmod
timestamp="$(date --iso-8601=seconds)"
sitemap=""
IFS=$'\r\n'
for line in $(cat ./sitemap.xml)
do
case $line in
*"<$field>"*"</$field>"* )
pre=${line#*"<$field>"}
suf=${line%"</$field>"*}
line="${line%$pre}${timestamp}${line#$suf}"
;;
esac
sitemap=$sitemap$line$'\n'
done
# echo $sitemap > sitemap.xml
echo $sitemap
The code above should read the file, replace needed tags and output them. Here I'm trying to concatenate strings of file for later saving. And also I'm trying to add \n
to each line.
But the output of this code doesn't have new lines:
<?xml version="1.0" encoding="UTF-8"?> <url> <lastmod>2020-08-22T15:35:47+03:00</lastmod> <priority>1.00</priority> </url> <url> <lastmod>2020-08-22T15:35:47+03:00</lastmod> <priority>0.80</priority> </url> </urlset>
What am I doing wrong?