0

I am parsing an kml file which contains data like that:

<when>2013-08-15T15:00:39.744-07:00</when>
<gx:coord>13.7457943 52.0683374 0</gx:coord>
<when>2013-08-15T15:01:39.868-07:00</when>
<gx:coord>15.7458125 52.063955 0</gx:coord>

actually I like to parse that in bash so that I get when and gx:coord in one loop:

I got this far:

cat test.kml | grep -e "gx:coord" | while read line 
do
   echo $line
done

Which gives me all coords, and on the same way I would get the whens, read in a array I might achieve what I want, but there must be a better way :) Thnx in advance.

Harry
  • 1,313
  • 3
  • 22
  • 37
  • Can you install `xmlstarlet` (a proper XML parsing/generating toolkit accessible from bash)? Anything not involving a proper parser, in general, be an evil hack (see also http://stackoverflow.com/a/1732454/14122). – Charles Duffy Aug 27 '13 at 18:33
  • To expound on *why* anything else would be a hack -- consider the case when your entries aren't separated by newlines; the KML format certainly doesn't require those newlines. Or consider the case when the namespace currently referred to as `gx` has a different prefix. Or the case where half of the `gx:coord` items are commented out, and you want to be able to exclude them. Or so forth. – Charles Duffy Aug 27 '13 at 18:34
  • By the way, to write an example of how to do this the right way, I'd need to know how the `xmlns:gx` attribute earlier in the file is set; the substrings given here aren't sufficient. – Charles Duffy Aug 27 '13 at 18:36

1 Answers1

4

With bash if your lines are in order:

while read WHEN && read GXCOORD; do
    echo "$WHEN : $GXCOORD"
done < test.kml

Or perhaps do it this way:

while read WHEN && read GXCOORD; do
    echo "$WHEN : $GXCOORD"
done < <(exec grep -Fe "<when>" -e "<gx:coord>" test.kml)

And also perhaps trim out the tags:

while read WHEN && read GXCOORD; do
    WHEN=${WHEN##*'<when>'} WHEN=${WHEN%%'</when>'*}
    GXCOORD=${GXCOORD##*'<gx:coord>'} GXCOORD=${GXCOORD%%'</gx:coord>'*}
    echo "$WHEN : $GXCOORD"
done < <(exec grep -Fe "<when>" -e "<gx:coord>" test.kml)
konsolebox
  • 72,135
  • 12
  • 99
  • 105