Question

I need to create a table from a gpx file extracting only important data. Here is a sample of the gpx file format.

<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1">
<metadata>
</metadata>
<trk>
<name><![CDATA[Track2013/08/05_12:43]]></name>
<trkseg>
<trkpt lat="36.084275" lon="-115.137618">
<ele>653.926645</ele>
<time>2013-08-05T16:43:07Z</time>
<desc><![CDATA[lat.=36.084275, lon.=-115.137618, Alt.=653.926645m. Speed=0.000000m/h.]]></desc>
</trkpt>
<trkpt lat="36.084036" lon="-115.137834">
<ele>644.502060</ele>
<time>2013-08-05T16:43:12Z</time>
<desc><![CDATA[lat.=36.084036, lon.=-115.137834, Alt.=644.502060m. Speed=0.000000m/h.]]></desc>
</trkpt>

Basically, I need to get the latitude, longitude, altitude (or ele), and the time in a table of this format:

    Latitude Longitude Altitude Time
1   number   number    number   hour
2   more     more      more     more
3   etc

I filtered out what I dont need using

awk -F: ' $1 ~ /lat="/ || $1 ~ /long="/ || $1 ~ /<ele>/ || $1 ~ /<time>/ || <trkseg> { print} ' gpxfile

it basically filters out what I dont need but I can't get it in the right formatting.

Was it helpful?

Solution

Setting up the field separators you can do:

$ awk -F'[<>=" ]' '
BEGIN{print "Latitude", "Longitude", "Altitude", "Time"}
/<trkpt / {printf "%s %s %s ", ++x,$5,$9}
/<ele>/ {printf "%s ", $3}
/<time>/ {print $3}' file
Latitude Longitude Altitude Time
1 36.084275 -115.137618 653.926645 2013-08-05T16:43:07Z
2 36.084036 -115.137834 644.502060 2013-08-05T16:43:12Z
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top