在Perl中读取和比较行
我在让perl脚本正常工作时遇到了问题。该问题可能与while循环中逐行读取提取文件有关,如有任何帮助,将不胜感激。有两个文件 包含错误ID列表的错误文件(100个ID) 2在Perl中读取和比较行,perl,Perl,我在让perl脚本正常工作时遇到了问题。该问题可能与while循环中逐行读取提取文件有关,如有任何帮助,将不胜感激。有两个文件 包含错误ID列表的错误文件(100个ID) 2 3 包含字段1中ID为的分隔数据的提取(百万行) 1 |数据|数据|数据 2 |数据|数据|数据 2 |数据|数据|数据 2 |数据|数据|数据 3 |数据|数据|数据 4 |数据|数据|数据 5 |数据|数据|数据 我正在尝试从ID匹配的大型提取文件中删除所有行。可以有多个ID匹配的行。摘录已分类 #use strict
3
包含字段1中ID为的分隔数据的提取(百万行) 1 |数据|数据|数据
2 |数据|数据|数据
2 |数据|数据|数据
2 |数据|数据|数据
3 |数据|数据|数据
4 |数据|数据|数据
5 |数据|数据|数据
我正在尝试从ID匹配的大型提取文件中删除所有行。可以有多个ID匹配的行。摘录已分类
#use strict;
#use warnning;
$SourceFile = $ARGV[0];
$ToRemove = $ARGV[1];
$FieldNum = $ARGV[2];
$NewFile = $ARGV[3];
$LargeRecords = $ARGV[4];
open(INFILE, $SourceFile) or die "Can't open source file: $SourceFile \n";
open(REMOVE, $ToRemove) or die "Can't open toRemove file: $ToRemove \n";
open(OutGood, "> $NewFile") or die "Can't open good output file \n";
open(OutLarge, "> $LargeRecords") or die "Can't open Large Records output file \n";
#Read in the list of bad IDs into array
@array = <REMOVE>;
#Loop through each bad record
foreach (@array)
{
$badID = $_;
#read the extract line by line
while(<INFILE>)
{
#take the line and split it into
@fields = split /\|/, $_;
my $extractID = $fields[$FieldNum];
#print "Here's what we got: $badID and $extractID\n";
while($extractID == $badID)
{
#Write out bad large records
print OutLarge join '|', @fields;
#Get the next line in the extract file
@fields = split /\|/, <INFILE>;
my $extractID = $fields[$FieldNum];
$found = 1; #true
#print " We got a match!!";
#remove item after it has been found
my $input_remove = $badID;
@array = grep {!/$input_remove/} @array;
}
print OutGood join '|', @fields;
}
}
#严格使用;
#使用警告;
$SourceFile=$ARGV[0];
$ToRemove=$ARGV[1];
$FieldNum=$ARGV[2];
$NewFile=$ARGV[3];
$LargeRecords=$ARGV[4];
打开(内嵌,$SourceFile)或死亡“无法打开源文件:$SourceFile\n”;
打开(删除$ToRemove)或死亡“无法打开ToRemove文件:$ToRemove\n”;
打开(OutGood,“>$NewFile”)或死“无法打开良好的输出文件\n”;
打开(unlarge,“>$LargeRecords”)或死亡“无法打开大型记录输出文件\n”;
#将坏ID列表读入数组
@数组=;
#循环检查每个不良记录
foreach(@array)
{
$badID=$\;
#逐行阅读摘录
while()
{
#把这条线分成两部分
@字段=拆分/\ \ 124;/,$;
我的$extractID=$fields[$FieldNum];
#打印“这是我们得到的:$badID和$extractID\n”;
而($extractID==$badID)
{
#写出大的坏记录
打印外部联接“|”和@fields;
#获取提取文件中的下一行
@字段=拆分/\ \ 124;/;
我的$extractID=$fields[$FieldNum];
$found=1;#正确
#打印“我们有一场比赛!!”;
#找到项目后将其删除
我的$input_remove=$badID;
@array=grep{!/$input_remove/}@array;
}
打印输出好的联接“|”和@字段;
}
}
首先,您很幸运:坏ID的数量很少。这意味着,您可以一次性读取坏ID列表,将它们粘贴到哈希表中,而不会遇到内存使用方面的任何困难。一旦将它们放入散列中,就可以逐行读取大数据文件,跳过坏ID的输出
#!/usr/bin/env perl
use strict;
use warnings;
# hardwired for convenience
my $bad_id_file = 'bad.txt';
my $data_file = 'data.txt';
my $bad_ids = read_bad_ids($bad_id_file);
remove_data_with_bad_ids($data_file, $bad_ids);
sub remove_data_with_bad_ids {
my $file = shift;
my $bad = shift;
open my $in, '<', $file
or die "Cannot open '$file': $!";
while (my $line = <$in>) {
if (my ($id) = extract_id(\$line)) {
exists $bad->{ $id } or print $line;
}
}
close $in
or die "Cannot close '$file': $!";
return;
}
sub read_bad_ids {
my $file = shift;
open my $in, '<', $file
or die "Cannot open '$file': $!";
my %bad;
while (my $line = <$in>) {
if (my ($id) = extract_id(\$line)) {
$bad{ $id } = undef;
}
}
close $in
or die "Cannot close '$file': $!";
return \%bad;
}
sub extract_id {
my $string_ref = shift;
if (my ($id) = ($$string_ref =~ m{\A ([0-9]+) }x)) {
return $id;
}
return;
}
#/usr/bin/env perl
严格使用;
使用警告;
#为方便而硬连线
我的$bad_id_文件='bad.txt';
我的$data_文件='data.txt';
my$bad\u id=读取\u bad\u id($bad\u id\u文件);
删除带有错误ID的错误数据($data\u file,$bad\u id);
子删除带有错误ID的数据{
我的$file=shift;
我的$bad=班次;
首先打开我的$in,“你很幸运:坏ID的数量很少。这意味着,你可以读取一次坏ID列表,将它们粘贴到哈希表中,而不会遇到内存使用方面的任何困难。一旦将它们放入哈希表中,你只需逐行读取大数据文件,跳过坏ID的输出
#!/usr/bin/env perl
use strict;
use warnings;
# hardwired for convenience
my $bad_id_file = 'bad.txt';
my $data_file = 'data.txt';
my $bad_ids = read_bad_ids($bad_id_file);
remove_data_with_bad_ids($data_file, $bad_ids);
sub remove_data_with_bad_ids {
my $file = shift;
my $bad = shift;
open my $in, '<', $file
or die "Cannot open '$file': $!";
while (my $line = <$in>) {
if (my ($id) = extract_id(\$line)) {
exists $bad->{ $id } or print $line;
}
}
close $in
or die "Cannot close '$file': $!";
return;
}
sub read_bad_ids {
my $file = shift;
open my $in, '<', $file
or die "Cannot open '$file': $!";
my %bad;
while (my $line = <$in>) {
if (my ($id) = extract_id(\$line)) {
$bad{ $id } = undef;
}
}
close $in
or die "Cannot close '$file': $!";
return \%bad;
}
sub extract_id {
my $string_ref = shift;
if (my ($id) = ($$string_ref =~ m{\A ([0-9]+) }x)) {
return $id;
}
return;
}
!/usr/bin/env perl
严格使用;
使用警告;
#为方便而硬连线
我的$bad_id_文件='bad.txt';
我的$data_文件='data.txt';
my$bad\u id=读取\u bad\u id($bad\u id\u文件);
删除带有错误ID的错误数据($data\u file,$bad\u id);
子删除带有错误ID的数据{
我的$file=shift;
我的$bad=班次;
打开我的$in,“我会使用如下哈希:
use warnings;
use strict;
my @bad = qw(2 3);
my %bad;
$bad{$_} = 1 foreach @bad;
my @file = qw (1|data|data|data 2|data|data|data 2|data|data|data 2|data|data|data 3|data|data|data 4|data|data|data 5|data|data|data);
my %hash;
foreach (@file){
my @split = split(/\|/);
$hash{$split[0]} = $_;
}
foreach (sort keys %hash){
print "$hash{$_}\n" unless exists $bad{$_};
}
其中:
我将使用如下哈希:
use warnings;
use strict;
my @bad = qw(2 3);
my %bad;
$bad{$_} = 1 foreach @bad;
my @file = qw (1|data|data|data 2|data|data|data 2|data|data|data 2|data|data|data 3|data|data|data 4|data|data|data 5|data|data|data);
my %hash;
foreach (@file){
my @split = split(/\|/);
$hash{$split[0]} = $_;
}
foreach (sort keys %hash){
print "$hash{$_}\n" unless exists $bad{$_};
}
其中:
试试这个:
$ perl -F'|' -nae 'BEGIN {while(<>){chomp; $bad{$_}++;last if eof;}} print unless $bad{$F[0]};' bad good
$perl-F'|'-nae'开始{while(){chomp;$bad{$}++;last if eof;}}除非$bad{$F[0]};'bad good
试试这个:
$ perl -F'|' -nae 'BEGIN {while(<>){chomp; $bad{$_}++;last if eof;}} print unless $bad{$F[0]};' bad good
$perl-F'|'-nae'开始{while(){chomp;$bad{$}++;last if eof;}}除非$bad{$F[0]};'bad good
我认为我遇到的问题是读取while循环中的下一行。我找到匹配项并被卡在循环中。我认为我遇到的问题是读取while循环中的下一行。我找到匹配项并被卡在循环中。谢谢您这样做,但是您能解释一下移位函数及其如何应用于h吗ere?我刚刚读了这篇文章:但是我想澄清一下。而且:传入的任何参数都会显示在数组@
中。谢谢你,这是有效的,但是你能解释一下移位函数及其在这里的应用吗?我刚刚读了这篇文章:但是我想澄清一下。而且:传入的任何参数都会显示在数组@ode>。