一个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;
| .* | .*;
要求:求出所有在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;
相关文章推荐
- 在处理大量文本的繁琐的操作时,perl语言无疑是一个好的选择
- Perl与Python处理文本数值的一个比较
- WMI via C++: 一个C++风格的复杂性处理实例
- iOS的界面触摸事件处理机制,然后用一个实例来说明下应用场景.
- Perl语言入门笔记 第九章 用正则表达式处理文本
- Problem Description 输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数。 Input 输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测试实例的数值的个数,跟着就是n个整数。n=0表示输入的结束,不做处理。 Output 对于每组
- perl文本处理总结[2]
- 用perl对文件中的字符串做处理的一个简单的示例
- 【selenium webdriver】 XPath 多条件查询语句(处理同一个页面上出现相同文本按钮的动态ID元素定位)
- Perl处理文本一例
- 正则表达式处理html实例(Perl实现)
- 发送一个SOAP请求后的处理过程-----实例
- 文本处理语言awk在线实例极速入门 (全三部分)
- 一个JavaScript处理textarea中的字符成每一行实例
- Perl 学习笔记-正则表达式处理文本
- 用PHP写的一个文本处理小程序
- 通过一个实例简单了解perl
- 一个文本处理小工具(原创)
- 一个用Spring AOP实现异常处理和记录程序执行时间的实例--邵京国
- 文本处理 - 每次一个字符