一个Perl文本处理实例
来源:互联网 发布:何不食肉糜现代版知乎 编辑:程序博客网 时间:2024/05/19 05:03
假设存在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 文本处理
- perl 处理文本
- perl mojo 处理文本
- Perl与Python处理文本数值的一个比较
- perl 处理excel 实例
- perl文本处理总结[2]
- perl处理文本经验积累
- perl处理文本经验积累(不断更新中)
- Linux 常用文本处理实例
- 在处理大量文本的繁琐的操作时,perl语言无疑是一个好的选择
- linux下perl处理文本---使用hash处理
- perl文本
- 通过一个实例简单了解perl
- Perl脚本同时只运行一个实例
- Perl语言学习笔记 9 正则表达式处理文本
- awk and sed 文本处理实例-01
- 记一次python文本处理实例
- JOIN用法简述
- 使用目录创建VC2003/VC2005工程
- c# aspx 导出Excel文件的方法
- ASP.NET 常見問題解答(二)
- PL/SQL 程序包中的EXIT和RETURN
- 一个Perl文本处理实例
- SaaS---ASP接入宝典
- 20081224
- Unlock files or folders using Unlocker
- 2008.12.24
- ffmpeg转PSP参数设置
- ffmpeg转ipod参数设置
- 我跟踪软件的下载和使用情况的方法
- 如何放置手机辐射才是最低