Skip to content

Instantly share code, notes, and snippets.

@RobCranfill
Last active January 29, 2019 02:56
Show Gist options
  • Save RobCranfill/9996de2c0b44172e3e8ded4f6eaa58d4 to your computer and use it in GitHub Desktop.
Save RobCranfill/9996de2c0b44172e3e8ded4f6eaa58d4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# [email protected] for Todd S
# uses 'xmlstarlet' to extract XML data to CSV
# fix input file - example
# sed 's/\x0d\xEF\xBB\xBF/\x0a/g' <ut-landmarks.gpx >ut-landmarks-good.gpx
# - will still have one ^M at start of file?
# rock art - this one is different
xmlstarlet sel -T -t -m /gpx/wpt -v "concat(@lat,',',@lon,',',name)" -n utah-rock-art.gpx >utah-rock-art.csv
# mines
xmlstarlet sel -T -t -m /gpx/wpt -v "concat(@lat, ',', @lon, ',', ele, ',', extensions)" -n ut-mines.gpx >ut-mines.csv
# land features
xmlstarlet sel -T -t -m /gpx/wpt -v "concat(@lat, ',', @lon, ',', ele, ',', extensions)" -n ut-land-features.gpx >ut-land-features.csv
# landmarks
xmlstarlet sel -T -t -m /gpx/wpt -v "concat(@lat, ',', @lon, ',', ele, ',', extensions)" -n ut-landmarks.gpx >ut-landmarks.csv
# towns
xmlstarlet sel -T -t -m /gpx/wpt -v "concat(@lat, ',', @lon, ',', ele, ',', extensions)" -n ut-towns.gpx >ut-towns.csv
#!/bin/bash
# Fun with xmlstarlet
# validate the file
xmlstarlet val ut-landmarks.gpx
# look at distinct ("unique" [sic]) elements
xmlstarlet el -u ut-landmarks.gpx
# look at attribute values
xmlstarlet el -v ut-landmarks.gpx
# format the XML
xmlstarlet fo utah-rock-art.gpx
# count something
xml sel -t -v "count(/gpx/trk)" -n utah-rock-art.gpx
# extract to CSV
xmlstarlet sel -T -t -m /gpx/wpt -v "concat(@lat, ',', @lon, ',', ele, ',', extensions)" -n ut-land-features.gpx >ut-land-features.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment