Skip to content

Instantly share code, notes, and snippets.

@amitpdev
Created September 16, 2015 17:17
Show Gist options
  • Save amitpdev/8a61cf1f4a43973e570b to your computer and use it in GitHub Desktop.
Save amitpdev/8a61cf1f4a43973e570b to your computer and use it in GitHub Desktop.
Use this awk script to adjust standard GPX files downloaded from any site (bikes) into a GPX format that is supported by Xcode.
awk '
BEGIN {
buffer=""
}
{
gsub(/<\/*trk>/,"",$0)
gsub(/<\/*trkseg>/,"",$0)
gsub(/<trkpt/,"<wpt", $0)
gsub(/<\/trkpt>/,"<\/wpt>", $0)
print
}
/<wpt/,/<\/wpt>/ {
buffer=buffer $0
if ($0 !~ "<\/wpt>") {
buffer=buffer "\n"
}
}
/<\/wpt>/ {
#print buffer
buffer=""
}
END {
}' $1
@nipun0505
Copy link

I am not sure how to process the file from bikehike with this script. Can you help. I tried with terminal in Mac but no luck.

@nipun0505
Copy link

OK, I tried: "awk -f script.awk bikehike_course > output.gpx", but got this error:

Error: awk: syntax error at source line 1 source file adjust_gpx_to_apple_format.awk context is awk >>> ' <<< awk: bailing out at source line 24

@kalidasweb
Copy link

awk 
BEGIN {
    buffer=""
}
{
    gsub(/<\/*trk>/,"",$0)
    gsub(/<\/*trkseg>/,"",$0)
    gsub(/<trkpt/,"<wpt", $0)
    gsub(/<\/trkpt>/,"<\/wpt>", $0)
    print
}
/<wpt/,/<\/wpt>/ {
    buffer=buffer $0
    if ($0 !~ "<\/wpt>") {
        buffer=buffer "\n"
    }
}
/<\/wpt>/ {
    #print buffer
    buffer=""
}

END {
} $1


this one working fine just remove some codes :) (crazy only single quotes 👍 ).

@pappalar
Copy link

All the buffer calculation is not needed.

The same effect can be achieved by editing the original file and printing the result (which is what the script is already doing, but by using print is printing twice the same things)

awk
{
    gsub(/<\/*trk>/,"",$0)
    gsub(/<\/*trkseg>/,"",$0)
    gsub(/<trkpt/,"<wpt", $0)
    gsub(/<\/trkpt>/,"<\/wpt>", $0)
}
END {}
$1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment