我有一个包含数据库记录的大型数据文件(文本)。 每个记录由“ - ”分隔(其中146个) 我想基于来自该块的数据将每个分隔符之间的文本写入新文件。新文件名应包含一段数据,然后是年份(即2017年)
例如,使用此示例文件:
1. 12/18/17
Company
PAGE 2
1:14 PM GET CURRENTLY SELECTED DEBTORS
------------------------------------------------------------------------------------------------------------------------
----------------------------- STATUS **Acct:1234** Disposition:9000 CANCEL Wait: 04/11/17
DEBTOR Name:Doe John
Ssn:123456789 Cbr: Ph:555-555-555
Rp:Doe John Ssn: Rp Ph:
Adr1:211 some road POE: Lgl: POE Ph:
City:anytown Cty: Canc:UNC Born:01/01/1937
St: VA Zip:54321 St: Zip: COF:N Sal:
Clnt:00248 Someplace, 93076 Org: 4
80.00
List:01/05/17 Srv:08/25/16 Pl95: Time:9 Calls:0 Con:0 Bal: 480.00
Co-Maker's Previous Address Spouse's Previous Address MULTIPLE
ACCOUNTS RM# Acct Name / Client Chk# / Lst Srv Lpy
Col Disp Bal Check Reason Drivers Licen se #
PRN INT LI3 LI4 AIN CC ATY MS1 PJI 1 142424* Doe John
93076/00248/Somewhere 01/05/17 08/25/16 0 9000
480.00
480.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
2. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
因此,需要提取此块并将其写入名为1234_2017.txt的新文件,该文件基于该记录的Acct:1234数据 - 然后移至下一个块。我已经计算了754条记录中第一个文件的记录数,而且我总共有10个这样的文件。
#! / usr/bin/perl -w
use strict;
use warnings;
use POSIX 'strftime';
my $filename = "Facs_Data.txt";
my $outfile = "Acct_".strftime '%Y', localtime;
my $start = "'-' x 146";
open (INFILE, "<$filename"); open (OUTFILE, ">$outfile");
while ($start) {
print $outfile;
next
}
close (INFILE);
close (OUTFILE);
任何建议都将不胜感激。
答案 0 :(得分:1)
如果你不想付出任何努力来解释你所尝试的内容,我不会付出任何努力来解释我的解决方案。
#!/usr/bin/perl
use strict;
use warnings;
local $/ = '>' . ('-' x 146) . "\n";
my $year = (localtime)[5] + 1900;
# <> reads from STDIN or a filename given as an argument
while (<>) {
next unless /\S/; # Ignore empty records (like the first one)
my ($acct) = /Acct:(\d+)/;
my $file = "${acct}_${year}.txt";
open my $fh, '>', $file
or die "Can't open $file: $!\n";
print $fh;
}