^zhurnaly   -   Random   -   Recent   -   Running   -   Mantra   -   Tarot   -   Help

Garmin File Extractor

It's been a long time since I wrote any code, but yesterday the old dog dusted off the Perl manual and managed a few simple tricks. The Garmin GPS unit that I have produces a *.tcx file which is a succession of XML-tagged data points. With every outing the file grows a megabyte or so in length, depending on the duration of the run. In order to produce maps and elevation profiles I've been using a text editor to strip out a single day's trek from the mass. But as the file gets larger, that's more and more inconvenient.

Hence, the following tiny Perl script to pull a run's trackfile out of an arbitrarily-large Garmin *.tcx file:

#! /usr/bin/perl

# extract_garmin.prl version 0.1 - ^z - 2010-04-20
# usage:  perl extract_garmin.prl datepattern <infile.tcx >outfile.tcx

# take a Garmin *.tcx file and extract a chunk for a given date
# example:  perl extract_garmin.prl 2010-04-10 <Garmin_2010-04-17.tcx >Garmin_BRR.tcx

# method: print out header down to and including line containing <Activity Sport="Running">
# scan until find line that matches datepattern
# print out that line and all subsequent lines until and including next </Activity>
# print out </Activities> and </TrainingCenterDatabase>, and finish

$datepattern = $ARGV[0];
while (<STDIN>) {
  print;
  last if /<Activity Sport="Running">/;
}
while (<STDIN>) {
  last if /$datepattern/;
}
print;
while (<STDIN>) {
  print;
  last if /<\/Activity>/;
}
print " </Activities>\n</TrainingCenterDatabase>\n";

^z - 2010-04-21