如何使用 Perl 匹配两个文件中的记录? [英] How can match records in two files using Perl?

查看:74
本文介绍了如何使用 Perl 匹配两个文件中的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件,CUSTOMER_ACCOUNT_LOG.TXT,CUSOMER_ID_LOG.TXT.

I have two files, CUSTOMER_ACCOUNT_LOG.TXT, CUSOMER_ID_LOG.TXT.

这是日志,维护时间戳和帐户ID,与另一个文件中的时间戳和客户ID相同,

Am this is log, maintain the timestamp and account id, same like in another file timestamp and customerid,

简单,我想选择具有匹配时间戳的 AccountID 和 CustomerID,

simple, i want to pick the AccountID and CustomerID with matched TIMESTAMP,

例如,123456793 是 TIMESTAMP,对于这个 Equlent 匹配记录是 ABC0103,CUSTOMER_ID_0103,

For example, 123456793 is TIMESTAMP, FOR this Equlent match records are ABC0103,CUSTOMER_ID_0103,

像这样我想选择详细的并且需要将这些匹配的记录写入另一个文件中,

like this i want to pick detaild and need to make these matched records wrtite into another file,

CUSOMER_ACCOUNT_LOG.TXT

CUSOMER_ACCOUNT_LOG.TXT

TIMESTAMP| N1| N2 |ACCOUNT ID
-----------------------------------
123456789,111,1000,ABC0101
123456791,112,1001,ABC0102
123456793,113,1002,ABC0103
123456795,114,1003,ABC0104
123456797,115,1004,ABC0105
123456799,116,1005,ABC0106
123456801,117,1006,ABC0107
123456803,118,1007,ABC0108
123456805,119,1008,ABC0109
123456807,120,1009,ABC0110
123456809,121,1010,ABC0111
123456811,122,1011,ABC0112
123456813123,1012,ABC0113
123456815,124,1013,ABC0114
123456817,125,1014,ABC0115
123456819,126,1015,ABC0116
123456821,127,1016,ABC0117
123456823,128,1017,ABC0118
123456825,129,1018,ABC0119
123456827,130,1019,ABC0120
123456829,131,1020,ABC0121

CUSOMER_ID_LOG.TXT

CUSOMER_ID_LOG.TXT

TIMESTAMP| N1| N2 | CUSTOMER ID
-----------------------------------
123456789,111,1000,CUSTOMER_ID_0101
123456791,112,1001,CUSTOMER_ID_0102
123456793,113,1002,CUSTOMER_ID_0103
123456795,114,1003,CUSTOMER_ID_0104
123456797,115,1004,CUSTOMER_ID_0105
123456799,116,1005,CUSTOMER_ID_0106
123456801,117,1006,CUSTOMER_ID_0107
123456803,118,1007,CUSTOMER_ID_0108
123456805,119,1008,CUSTOMER_ID_0109
123456807,120,1009,CUSTOMER_ID_0110
123456809,121,1010,CUSTOMER_ID_0111
123456811,122,1011,CUSTOMER_ID_0112
123456813123,1012,CUSTOMER_ID_0113
123456815,124,1013,CUSTOMER_ID_0114
123456817,125,1014,CUSTOMER_ID_0115
123456819,126,1015,CUSTOMER_ID_0116
123456821,127,1016,CUSTOMER_ID_0117
123456823,128,1017,CUSTOMER_ID_0118
123456825,129,1018,CUSTOMER_ID_0119
123456827,130,1019,CUSTOMER_ID_0120
123456829,131,1020,CUSTOMER_ID_0121

我是一名 PHP 程序员,刚接触 Perl.

I am a PHP programer, and new to Perl.

首先我读取文件,然后我只是制作了数组,现在我的数组包含所需细节的其余部分的时间戳,实际上应该知道什么?我们应该将文件和填充值读入数组,所以猜测,数组键应该包含帐户ID,数组值应该是时间戳,反之亦然不确定,就像另一个文件一样,最后我们应该比较时间戳,哪些时间戳匹配然后时间戳帐户我们应该选择 id 和客户 id,据我所知,我填充了数组,现在我不知道如何进一步进行,因为,这里应该使用 foreach,然后需要匹配 noth 文件时间戳,我卡在这里!

First i read the file, and then i just maded array, now my array contains the timestampe rest of the required details, actually what should do know ? we should read the file and fille values into array, so guess, array key should contain Account id and array value should timestamp wise versa not sure, same like another file, finally we should compare the time stamp, which timestamps are matched then timestamps account id and customer id we should pick, upto my knowledge i filled the array, now i dont knwo how to proceed further, because, here should use the foreach and then need to match noth file timestamps, am stuck here !

推荐答案

以下是我要采取的步骤:

Here are the steps I'd take:

0) 一些基本的 Perl 样板(这是第 0 步,因为您应该始终始终这样做,有些人会向该样板中添加其他内容,但这是最起码的):

0) Some rudimentary Perl boilerplate (this is step 0 because you should always always always do it, and some people will add other stuff to this boilerplate, but this is the bare minimum):

use strict;
use warnings;
use 5.010;

1) 将第一个文件读入一个哈希,其键是时间戳:

1) Read the first file into a hash whose keys are the timestamp:

my %account;
open( my $fh1, '<', $file1 ) or die "$!";
while( my $line = <$fh1> ) {
  my @values = split ',', $line;
  $account{$values[0]} = $values[3];
}
close $fh1;

2)读取第二个文件,每读取一行,拉出时间戳,然后将时间戳、账户ID和客户ID打印到一个新文件中.

2) Read the second file, and each time you read a line, pull out the timestamp, then print out the timestamp, the account ID, and the customer ID to a new file.

open( my $out_fh, '>', $outfile ) or die "$!";
open( my $fh2, '<', $file2 ) or die "$!";
while( my $line = <$fh2> ) {
  my @values = split ',', $line;
  say $out_fh join ',', $values[0], $account{$values[0]}, $values[3];
}
close $out_fh;
close $fh2;

您不想将整个文件读入数组,因为那样会浪费内存.仅存储您需要的信息,并利用 Perl 的数据类型来帮助您存储该信息.

You don't want to read the whole file into an array because that's a waste of memory. Only store the information that you need, and take advantage of Perl's datatypes to help you store that information.

这篇关于如何使用 Perl 匹配两个文件中的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆