Expect OP needs to add the entire <name>...</name> stanza rather than just add $TRIPNAME between an existing such one. If so sed's "i" operator will come in handy. E.g.
Code: Select all
rene@hp8k:~$ cat test.gpx
<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="JGxxxxx">
-<trk>
<trkseg>
<trkpt lon="174.90728760" lat="-41.28062820">
<ele>-102.724</ele>
<time>2001-10-23T20:55:11.000Z</time>
</trkpt>
rene@hp8k:~$ TRIPNAME=Trip
rene@hp8k:~$ sed "/<trkseg>/i\ <name>$TRIPNAME</name>" test.gpx
<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="JGxxxxx">
-<trk>
<name>Trip</name>
<trkseg>
<trkpt lon="174.90728760" lat="-41.28062820">
<ele>-102.724</ele>
<time>2001-10-23T20:55:11.000Z</time>
</trkpt>
You can of course redirect the output to a result file, or alternatively use
sed -i
instead to edit in this case "test.gpx" in-place.
[EDIT] Note; edited out a typo.