Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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>
share|improve this question
1  
Can you please post some sample content of your xml file? –  Guru Jan 22 '13 at 3:18
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
looks like that did the trick — thanks! –  Ben L. Jan 22 '13 at 4:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.