Perl 使用 sort, hash解析文件代码
2011-07-18 18:03
246 查看
同事写的简单的Perl代码
#!/usr/bin/perl -w
use strict;
my $pbid_index = 0;
my $lbid_index = 3;
my $ver_index = 5;
my $uuid_index = 6;
&test();
sub my_sort {
my %hash;
my @keys;
my @sorted_keys;
my $uuid;
my $last_uuid;
open(M_FILE, "<middlefile");
#read files
while (<M_FILE>) {
my @file_line = split(/:/);
my $logid = $file_line[$lbid_index];
my $flag = -1;
if ($file_line[$pbid_index] ne "pbid") {
next;
}
#find uuid
$uuid = $file_line[$uuid_index];
if (!$uuid) {
next;
} elsif (!$last_uuid) {
$last_uuid = $uuid;
} elsif (eof || ($last_uuid ne $uuid)) {
#sort file
@keys = keys %hash;
@sorted_keys = sort {$a <=> $b} @keys;
#open new file
open NEW_FILE, '>file_'.$last_uuid;
#write files
foreach (@sorted_keys) {
print NEW_FILE $hash{$_};
}
#close files
close NEW_FILE;
$last_uuid = $uuid;
undef %hash;
undef @keys;
undef @sorted_keys;
}
if (exists $hash{$logid}) {
my $new_ver = hex($file_line[$ver_index]);
my @old_line = split(/:/, $hash{$logid});
my $old_ver = hex($old_line[$ver_index]);
if ($new_ver > $old_ver) {
$hash{$logid} = $_;
}
} else {
$hash{$logid} = $_;
}
}
close M_FILE;
}
sub sort_uuid {
my $uindex = 7;
my $cmd = "sort -d -t : -k $uindex -o middlefile tmp";
my $TMP;
my @line;
print $uindex;
open $TMP, '>tmp';
while (<>) {
@line = split(/:/);
if ( 'pbid' ne $line[0]) {
next;
}
print $TMP $_;
}
close $TMP;
system($cmd);
system('rm -f tmp');
}
sub test {
&sort_uuid();
&my_sort();
}
#!/usr/bin/perl -w
use strict;
my $pbid_index = 0;
my $lbid_index = 3;
my $ver_index = 5;
my $uuid_index = 6;
&test();
sub my_sort {
my %hash;
my @keys;
my @sorted_keys;
my $uuid;
my $last_uuid;
open(M_FILE, "<middlefile");
#read files
while (<M_FILE>) {
my @file_line = split(/:/);
my $logid = $file_line[$lbid_index];
my $flag = -1;
if ($file_line[$pbid_index] ne "pbid") {
next;
}
#find uuid
$uuid = $file_line[$uuid_index];
if (!$uuid) {
next;
} elsif (!$last_uuid) {
$last_uuid = $uuid;
} elsif (eof || ($last_uuid ne $uuid)) {
#sort file
@keys = keys %hash;
@sorted_keys = sort {$a <=> $b} @keys;
#open new file
open NEW_FILE, '>file_'.$last_uuid;
#write files
foreach (@sorted_keys) {
print NEW_FILE $hash{$_};
}
#close files
close NEW_FILE;
$last_uuid = $uuid;
undef %hash;
undef @keys;
undef @sorted_keys;
}
if (exists $hash{$logid}) {
my $new_ver = hex($file_line[$ver_index]);
my @old_line = split(/:/, $hash{$logid});
my $old_ver = hex($old_line[$ver_index]);
if ($new_ver > $old_ver) {
$hash{$logid} = $_;
}
} else {
$hash{$logid} = $_;
}
}
close M_FILE;
}
sub sort_uuid {
my $uindex = 7;
my $cmd = "sort -d -t : -k $uindex -o middlefile tmp";
my $TMP;
my @line;
print $uindex;
open $TMP, '>tmp';
while (<>) {
@line = split(/:/);
if ( 'pbid' ne $line[0]) {
next;
}
print $TMP $_;
}
close $TMP;
system($cmd);
system('rm -f tmp');
}
sub test {
&sort_uuid();
&my_sort();
}
相关文章推荐
- swift:使用NSXMLParser解析本地xml文件(详细代码注释)
- Perl使用File::Basename获取文件扩展名的代码
- JAVA代码-使用DOM4J解析复杂XML文件
- swift:使用NSJSONSerialization解析本地json数据文件(代码详细注释)
- perl 解析xml ,json文件及curl工具的使用
- 使用Perl创建指定编码格式(如utf-8)文件的实现代码
- 使用ftp上传文件代码解析
- 使用XPathExpression的AddSort方法获得排序的Xml解析结果的代码
- 使用perl清理电脑上重复文件实现代码(续)
- java代码使用ImageJ解析dicom文件成图片
- Django中Forms的使用代码解析
- PE文件解析代码
- salesforce 零基础学习(五十三)多个文件生成一个zip文件(使用git上封装的代码)
- 在C++控制台项目中使用C工程代码时的注意,避免出现error LNK2019: 无法解析的外部符号
- 在vim中使用perltidy美化perl代码
- 使用Python库ElementTree解析Hadoop的xml配置文件
- 使用DOM解析XML文件
- 在vim中使用perltidy美化perl代码
- 使用JAVA代码实现文件上传到阿里云
- 使用代码辅助生成工具CodeSmith -- 生成NHibernate的映射文件