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.

4

1 回答 1

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)
于 2013-08-27T18:28:58.043 回答