How to programmatically combine ways with identical tags

I’ve got an .osm file that is the result of a conversion from shape files and I would like to know how to programmatically combine ways with identical tags and common nodes. I can combine ways in JOSM but since I have 1,411 roads split into many more ways, I thought it would save tons of time to combine them programmatically.

One possible solution is https://svn.openstreetmap.org/applications/utils/import/OsmGlommer/

Be careful not combining ‘identical’ ways that are not identical in being part of (different) route relations!

Be warned to use OsmGlommer!

See the source code https://svn.openstreetmap.org/applications/utils/import/OsmGlommer/OsmGlommer/Form1.cs and you will see, that the word “relation” is not mentioned once. This means that OsmGlommer does not know what an relation is.

I have tested OsmGlommer and the result was very, very bad for relations. The relation is just wrong afterwards or the relation has members which OsmGlommers deleted by joining ways. Josm denies to open such an corrupt osm-file.

Use this example file for input, if you want see the trash

<osm version='0.6' generator='JOSM'>
  <node id='-39203' action='modify' lat='47.71264102887' lon='12.86052412842' />
  <node id='-39205' action='modify' lat='47.71480874974' lon='12.88694301948' />
  <node id='-39207' action='modify' lat='47.7156221727' lon='12.91295392568' />
  <node id='-39209' action='modify' lat='47.70315060092' lon='12.8851379672' />
  <node id='-39211' action='modify' lat='47.71442440273' lon='12.87498210172' />
  <node id='-39269' action='modify' lat='47.71430163177' lon='12.88106444479' />
  <node id='-39287' action='modify' lat='47.71389239317' lon='12.8676224666' />
  <node id='-39353' action='modify' lat='47.71503825316' lon='12.90064958947' />
  <node id='-39369' action='modify' lat='47.70934962886' lon='12.88593031924' />
  <way id='-39213' action='modify'>
    <nd ref='-39203' />
    <nd ref='-39287' />
    <nd ref='-39211' />
    <tag k='highway' v='path' />
    <tag k='name' v='Test' />
  </way>
  <way id='-39215' action='modify'>
    <nd ref='-39205' />
    <nd ref='-39353' />
    <nd ref='-39207' />
    <tag k='highway' v='path' />
    <tag k='name' v='Test' />
  </way>
  <way id='-39217' action='modify'>
    <nd ref='-39209' />
    <nd ref='-39369' />
    <nd ref='-39205' />
    <tag k='highway' v='path' />
  </way>
  <way id='-39219' action='modify'>
    <nd ref='-39211' />
    <nd ref='-39269' />
    <nd ref='-39205' />
    <tag k='highway' v='path' />
    <tag k='name' v='Test' />
  </way>
  <relation id='-39221' action='modify'>
    <member type='way' ref='-39217' role='' />
    <member type='way' ref='-39215' role='' />
    <tag k='network' v='lwn' />
    <tag k='route' v='hiking' />
    <tag k='type' v='route' />
  </relation>
</osm>