我有一份题为“频率.xml”的文件,其中载有这一表格的内容:
<?xml version="1.0"?>
<!DOCTYPE stationlist PUBLIC "-//xxxxx//DTD stationlist 1.0//EN" "http://xxxxxxxxx/DTD/xxxxxxxx.dtd">
<frequencies xmlns="http://xxxxxxxxxxxxxxxx/DTD/">
<list norm="PAL" frequencies="Custom" audio="bg">
..............................................................
<station name="A" active="1" channel="48.25MHz" norm="PAL"/>
<station name="B" active="1" channel="55.25MHz" norm="PAL"/>
<station name="C" active="1" channel="62.25MHz" norm="PAL"/>
<station name="D" active="1" channel="112.25MHz" norm="PAL"/>
..............................................................
<station name="E" active="1" channel="119.25MHz" norm="PAL"/>
<station name="F" active="0" channel="48.25MHz" norm="PAL"/>
..............................................................
<station name="G" active="1" channel="55.25MHz" norm="PAL"/>
<station name="H" active="0" channel="62.25MHz" norm="PAL"/>
..............................................................
</list>
</frequencies>
如果含有与另一行相同的频率,我想删除被认为重复的线。
产出结果:
<station name="A" active="1" channel="48.25MHz" norm="PAL"/>
<station name="B" active="1" channel="55.25MHz" norm="PAL"/>
<station name="C" active="1" channel="62.25MHz" norm="PAL"/>
<station name="D" active="1" channel="112.25MHz" norm="PAL"/>
<station name="E" active="1" channel="119.25MHz" norm="PAL"/>
我写这封信:
for i in `cat frequencies.xml | sed s/.*channel="([^"]*)".*/1/; /</ d |grep MHz`; do
cat frequencies.xml | awk -v i="channel="$i"
BEGIN { a=0 }
$0 ~ i { if ( a == "1" ) { print i"" - duplicate" > "/dev/stderr" ; next ;} ; a=1 }
{ print $_ } > frequencies.xml.tmp &&
mv frequencies.xml.tmp frequencies.xml
done
如何用硬性语言来处理这个问题?
增 编
最新情况:我想保持XML的结构。
我的法典:
open (FH, "+< frequencies.xml") or die "Opening: $!";
my $out = ;
my %seen = ();
foreach my $line ( <FH> ) {
if ( $line =~ m/<station/ ) {
my ( $freq ) = ( $line =~ m/channel="([^"]+)"/ );
$out .= $line unless $seen{$freq}++;
} else {
$out .= $line;
}
}
seek(FH,0,0) or die "Seeking: $!";
print FH $out or die "Printing: $!";
truncate(FH, tell(FH)) or die "Truncating: $!";
close(FH) or die "Closing: $!";