0

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?

Yoskutik
  • 1,859
  • 2
  • 17
  • 43

3 Answers3

1

I did not look at 'what you are doing wrong', but this is an alternative (you only need to adapt the format of the date):

$ xmlstarlet ed -u //lastmod -v "`date`" sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset>
  <url>
    <lastmod>Sat Aug 22 14:51:59 CEST 2020</lastmod>
    <priority>1.00</priority>
  </url>
  <url>
    <lastmod>Sat Aug 22 14:51:59 CEST 2020</lastmod>
    <priority>0.80</priority>
  </url>
</urlset>

like this:

$ xmlstarlet ed -u //lastmod -v "$(date --iso-8601=seconds)" sitemap.xml
<?xml version="1.0" encoding="UTF-8"?>
<urlset>
  <url>
    <lastmod>2020-08-22T14:53:46+02:00</lastmod>
    <priority>1.00</priority>
  </url>
  <url>
    <lastmod>2020-08-22T14:53:46+02:00</lastmod>
    <priority>0.80</priority>
  </url>
</urlset>
Luuk
  • 12,245
  • 5
  • 22
  • 33
  • Thanks for the alternative. But I want to run it in the pre-commit git hook via `git-bash`. And `git-bash` doesn't have `xmlstarlet` – Yoskutik Aug 22 '20 at 12:55
  • 1
    LOL, it is available for Linux and windows, see: http://xmlstar.sourceforge.net/download.php – Luuk Aug 22 '20 at 13:01
  • Maybe. But git-bash says `bash: xmlstarlet: command not found` – Yoskutik Aug 22 '20 at 13:13
1

Replace echo $sitemap with echo "$sitemap"

alef
  • 124
  • 4
0

Use Quotes to Preserve or Append Newlines

Shell quoting and expansion rules can be tricky. You need to quote your assignment to sitemap. For example:

sitemap="$sitemap$line"

You also will likely need to quote your arguments to echo. If you really want to append a newline to the end of your concatenated string, you either have to perform the expansion outside the quotes, use the -e flag with echo, or just perform another echo. You could also use printf. As examples:

  • echo "${sitemap}${line}"$'\n'
  • echo -e "${sitemap}${line}\n"
  • printf "%s%s\n" "$sitemap" "$line"

If you need the newlines elsewhere you can move them around, but the principles will still be the same.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199