您的位置:首页 > 其它

一个Perl文本处理实例

2008-12-24 17:19 232 查看
假设存在file1, file2两个文本文件, 文件每行格式为:
| .* | .*;
要求:求出所有在file2文件中出现的行, 且仅保留双"|"之间的内容。
如: 若存在 |adfdf| dofijaosdjioaf 于file2, file1中不存在该项,则该返回 |adfdf|

参考例程:
#!/usr/bin/perl
use strict;
use warnings;

die "lack compare file/n" if(@ARGV < 2);
my ($file1, $file2) = ($ARGV[0], $ARGV[1]);
my $outfile = "result.out"; # storage result

#-------- format the input file into like this : |.*|
$^I = ".bak"; # make a copy
while(<>)
{
s/[^|]*$//;
print;
print "/n";
}
#-----------

system "diff $file1 $file2 > temp.out";
open TEMP, "<temp.out" or die "Can not open temporary file/n";
open OUT, ">$outfile" or die "Can not open file:$outfile/n";

my $count=0;
while(<TEMP>)
{
chomp;
if(//>(.*)/)
{
$count++;
print OUT "$1/n";
}
}
print OUT "total sums = $count/n";
close TEMP;
unlink "temp.out";
close OUT;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: