Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@nipun0505 nipun0505 commented Jun 1, 2017

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

This comment has been minimized.

Copy link

@nipun0505 nipun0505 commented Jun 2, 2017

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

This comment has been minimized.

Copy link

@kalidasweb kalidasweb commented Dec 8, 2017

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 👍 ).

@racer1988

This comment has been minimized.

Copy link

@racer1988 racer1988 commented Jun 10, 2019

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
You can’t perform that action at this time.