How to delete a part of a file

February 6, 2010 at 04:23:57
Specs: Windows XP
I have a GPX-file form my gps. I want to delete sections of it. a part of it is:

<name>001</name>
<sym>Dot</sym>
<type>School</type>
<extensions>
<label xmlns="http://www.topografix.com/GPX/gpx_overlay/0/3">
</label>
</extensions>
</wpt>
<wpt lat="55.54934616" lon="11.94559830">
<ele>48.430</ele>
<time>2008-05-07T22:09:25Z</time>
<name>002</name>
<sym>Dot</sym>
<type>School</type>
<extensions>
<text xmlns="http://www.topografix.com/GPX/gpx_style/0/2">
<family generic="sans-serif">
<face>Arial</face>
</family>
</text>
</extensions>
</wpt>
<trk>

I have tried to make a script in Perl to read the gpx-file line by line:

#!C:\\perl/bin/perl.exe
open(IndFil,"avnso1.gpx")|| die;
while ($line = <IndFil>)
{
print $line;
}
close(IndFil);
-but of course, that's not enough.

How do I delete the code between <extensions> and </extensions> - and the word <extensions></extensions>?
I would like it in Peal or PHP.


See More: How to delete a part of a file

Report •


#1
February 6, 2010 at 05:26:33
I haven't done perl for a looooonnng time, so my attempt
below is probably totally rubbish, but try it if you want.

#!C:\\perl/bin/perl.exe
open(IndFil,"avnso1.gpx")|| die;
$inExtensions = 0
while ($line = <IndFil>)
{
    if ($line == "<extensions>")
        $inExtensions = 1
    else if ($line == "</extensions>")
        $inExtensions = 0
    else if (!($inExtensions))
        print $line;
}
close(IndFil);


Report •

#2
February 6, 2010 at 06:16:36
Thank you!
I have made some corrections for syntaxerros, but it does not work. It sees that alle the lines go into the: if ($line == "<extensions>")

Here is the full code:

#!C:\\perl/bin/perl.exe
open(IndFil,"avnso1.gpx")|| die;
$inExtensions = 0;
while ($line = <IndFil>)
{
if ($line == "<extensions>") {
$inExtensions = 1;
}
elsif ($line == "</extensions>"){
$inExtensions = 0;
}
elsif (!($inExtensions)){
print $line;
}
else
{
print "no hits";
}
}
close(IndFil);


Report •

#3
February 6, 2010 at 10:46:53
If you include 'Perl' in the subject, it will make it easier for us to know what you need.

Here's a very simple 1 liner:

C:\>perl -ni.bak -e "print unless m|<extensions>| .. m|</extensions>|" avnso1.gpx

Or, if youwant that in script form:

#!/usr/bin/perl

use warnings;
use strict;

$^I = '.bak';
while ( <> ) {
    print unless m|<extensions>| .. m|</extensions>|;
}


Report •

Related Solutions

#4
February 7, 2010 at 02:44:35
I need it a script. But where do I place the gpx-filename: anvso1.gpx in the script?

Report •

#5
February 9, 2010 at 07:41:52
If you use the script I posted, then you would pass the filename as an argument to the script.

Like so,

C:\>script.pl anvso1.gpx


Report •


Ask Question