您好我有一个我需要解析的文本文件
libA/cellA/view1/data.dm#1 - edit default
libA/cellA/view1/master.tag#1 - edit defau
libA/cellA/view1/sch.oa#1 - edit default c
libA/cellA/view1/thumbnail_128x128.png#1 -
libA/cellB/view1/data.dm#1 - edit default
libA/cellB/view1/master.tag#1 - edit defau
libA/cellB/view1/sch.oa#1 - edit default c
libA/cellB/view1/thumbnail_128x128.png#1 -
libA/cellB/symbol/master.tag#1 - edit defa
libA/cellB/symbol/symbol.oa#1 - edit defau
libA/cellB/symbol/thumbnail_128x128.png#1
libA/cellC/view1/data.dm#1 - edit default
libA/cellC/view1/master.tag#1 - edit defau
libA/cellC/view1/sch.oa#1 - edit default c
libA/cellC/view1/thumbnail_128x128.png#1 -
libA/cellD/veriloga/data.dm#1 - edit defau
libA/cellD/veriloga/master.tag#1 - edit de
libA/cellD/veriloga/netlist.oa#1 - edit de
libA/cellD/veriloga/veriloga.va#1 - edit d
libA/cellE/view1/data.dm#1 - edit default
libA/cellE/view1/master.tag#1 - edit defau
步骤1:在上面的文本文件中,我只想保留文本直到第三次出现' /'字符。例如输出应如下所示:
libA/cellA/view1
libA/cellA/view1
libA/cellA/view1
libA/cellA/view1
libA/cellB/view1
libA/cellB/view1
libA/cellB/view1
libA/cellB/view1
libA/cellB/view2
libA/cellB/view2
libA/cellB/view2
libA/cellC/view1
libA/cellC/view1
libA/cellC/view1
libA/cellC/view1
libA/cellD/view3
libA/cellD/view3
libA/cellD/view3
libA/cellD/view3
libA/cellE/view1
libA/cellE/view1
Setp2:然后我想删除两行之间保留空行的重复行:
libA/cellA/view1
libA/cellB/view1
libA/cellB/view2
libA/cellC/view1
libA/cellD/view3
libA/cellE/view1
关于如何在Perl中执行此操作的任何指针? 我正在尝试这样的事情:
#!/usr/bin/perl
# File: parse.pl
unless (open(MYFILE, "sample.txt"))
{
die ("cannot open input file file1\n");
}
unless (open(OUTFILE, ">outfile.txt"))
{
die ("cannot open output file outfile\n");
}
# if the program gets this far, the file was
# opened successfully
$line = <MYFILE>;
while ($line ne "")
{
#print ($line);
my ($lib, $cell, $view) = split /:/, $line;
#print "$lib\n";
#print "$cell\n";
#print "$view\n";
print OUTFILE ($lib/$cell/$view/n);
$line = <MYFILE>;
}
答案 0 :(得分:1)
另存为script.pl
(或其他)并调用如下:perl script.pl input.txt
use strict;
use warnings;
my %unique;
while (<>) {
my @parts = split(/\//, $_);
my $key = join('/', @parts[0 .. 2]);
if (!$unique{$key}++) {
print $key;
}
print "\n";
}
答案 1 :(得分:0)
就是这样的
<强>代码:强>
my %unique = ();
my @fields = undef;
while(<DATA>){
chomp;
@fields = split /\//, $_;
if (not $unique{"$fields[0]/$fields[1]/$fields[2]"}++){
print "$fields[0]/$fields[1]/$fields[2]"
}else{
print "\n";
}
}
#map {print $_."\n"} keys %unique;
__DATA__
libA/cellA/view1/data.dm#1 - edit default
libA/cellA/view1/master.tag#1 - edit defau
libA/cellA/view1/sch.oa#1 - edit default c
libA/cellA/view1/thumbnail_128x128.png#1 -
libA/cellB/view1/data.dm#1 - edit default
libA/cellB/view1/master.tag#1 - edit defau
libA/cellB/view1/sch.oa#1 - edit default c
libA/cellB/view1/thumbnail_128x128.png#1 -
libA/cellB/symbol/master.tag#1 - edit defa
libA/cellB/symbol/symbol.oa#1 - edit defau
libA/cellB/symbol/thumbnail_128x128.png#1
libA/cellC/view1/data.dm#1 - edit default
libA/cellC/view1/master.tag#1 - edit defau
libA/cellC/view1/sch.oa#1 - edit default c
libA/cellC/view1/thumbnail_128x128.png#1 -
libA/cellD/veriloga/data.dm#1 - edit defau
libA/cellD/veriloga/master.tag#1 - edit de
libA/cellD/veriloga/netlist.oa#1 - edit de
libA/cellD/veriloga/veriloga.va#1 - edit d
libA/cellE/view1/data.dm#1 - edit default
libA/cellE/view1/master.tag#1 - edit defau
<强>输出:强>
libA/cellA/view1
libA/cellB/view1
libA/cellB/symbol
libA/cellC/view1
libA/cellD/veriloga
libA/cellE/view1