0

I'd like to change the date format in the contents of an XML file from 2013-01-21 to January 21

The following script works on my mac, but not on a linux server (I'll be running it as a daily cron job).

#!/bin/bash

while read line; do
  case "$line" in
    '<date>'*)
      echo '<date>'
      date -j -f %F `echo "$line" | sed -e 's/<date>//' -e 's-</date>--'` '+%B %d'
      echo '</date>'
      ;;
    *)
      echo "$line"
      ;;
  esac
done < file.xml > newfile.xml

The error I'm getting is date: illegal option -f

Is there a way to get this working on a linux server?

snippet from XML file:

  <show>
    <recordKey>SWTZ-TD-FA32DE3DE4701567</recordKey>
    <name><![CDATA[Turnstyle Music Group Presents: bandname]]></name>
    <city><![CDATA[New York]]></city>
    <venueName><![CDATA[The National Underground]]></venueName>
    <venueNameExt><![CDATA[Downstairs]]></venueNameExt>
    <showType><![CDATA[Bar / Pub]]></showType>

    <venueZip></venueZip>
    <venuePhone></venuePhone>
    <venueAddress><![CDATA[159 E Houston St.]]></venueAddress>
    <ticketURI><![CDATA[]]></ticketURI>
    <description><![CDATA[]]></description>
    <ageLimit>21+</ageLimit>
    <venueURI><![CDATA[http://www.thenationalunderground.com]]></venueURI>
    <ticketPrice><![CDATA[$5 , $10 at the door]]></ticketPrice>

    <date>2011-09-30</date>
    <timeSet>20:30:00.0000000</timeSet>
    <gmtDate>2011-10-01 00:30:00</gmtDate>
    <showtimeZone>America/New_York</showtimeZone>
    <timeDoors></timeDoors>
    <directLink><![CDATA[]]></directLink>
    <posterImage></posterImage>
    <lastUpdate>2011-09-21 10:46:23</lastUpdate>

    <stateAbbreviation>NY</stateAbbreviation>
    <state>New York</state>
    <countryAbbreviation>US</countryAbbreviation>
    <country>United States</country>
    <timeZone>America/New_York</timeZone>
    <deposit>No Deposit</deposit>
    <depositReceived><![CDATA[0.00]]></depositReceived>


    <artistname><![CDATA[bandname]]></artistname>
    <artistKey>AR-96F8FB907FA90202</artistKey>
  </show>
Ben L.
  • 3
  • 3

1 Answers1

0

This is happening because the OS X implementation of the date command is taken from the BSD family. Since you're only feeding in one line at a time, it should be safe to use the -d parameter instead:

date -d `echo "$line" | sed -e 's/<date>//' -e 's-</date>--'` '+%B %d'

This is assuming that your input format remains compatible between the two date implementations. If it's not, you'll need to get it in a different format first.

Andrew B
  • 32,588
  • 12
  • 93
  • 131