我想将文件拆分为两个文件。
如果文件名为example.txt
,则会将其分为两个文件,如EX1.txt
或EX2.txt
。
拆分取决于每行中的第二个字段。示例:如果HDR行有TEA003890459
作为第二个字段,则输出将转到EX1.txt
但如果HDR有TEA003886004
,则输出进入EX2.txt
。
我还想计算索赔号。
我想使用以下逻辑来做到这一点:
if Header-Row then
if Dummy cost center then
write to Gas file
keep in mind: Claim-Nummer (eg. Array or Hash)
else
write to normal file
end if
else if Detail-Row then
if kept Claim-Nummer then
write to Gas file
else
write to normal file
end if
end if
该文件包含以下数据:
HDR^TEA003890459^082582^Mohd Jamil^Jamili Fahmi Bin^^458^+^92000^+^92000^+^0000^+^0000^+^0000^^0^^0^^0^^0^^0^^0^20130307^^^^^^^222^MY0BD^2^jamilifahmi.mohdjamil@se1.bp.com^ MY0BCC#6482362304
DTL^TEA003890459^E^MY0BCC#6482362304 641301137^+^47000^MFA^20130209^Medical Expenses [Family]^^^0^^0^^0^^0^^0^^0^^0^^0^^C16Medical
DTL^TEA003890459^E^MY0BCC#6482362304 641301137^+^45000^MGE^20130304^Medical Expenses (Employee clinica^^^0^^0^^0^^0^^0^^0^^0^^0^^C16Medical
HDR^TEA003886004^082770^Bin Omar^Mohamad Fadzlizam^^458^+^135800^+^135800^+^0000^+^0000^+^0000^^0^^0^^0^^0^^0^^0^20130307^^^^^^^222^MY0BD^4^mohamad.omar@se1.bp.com^ MY0BCC#6485163100
DTL^TEA003886004^E^MY0BCC#6485163100 641301137^+^25000^MFA^20130221^Medical Expenses [Family]^^^0^^0^^0^^0^^0^^0^^0^^0^^C16Claim
DTL^TEA003886004^E^MY0BCC#6485163100 641301137^+^37150^MFA^20130224^Medical Expenses [Family]^^^0^^0^^0^^0^^0^^0^^0^^0^^C16Claim
DTL^TEA003886004^E^MY0BCC#6485163100 641301137^+^23650^MFA^20130226^Medical Expenses [Family]^^^0^^0^^0^^0^^0^^0^^0^^0^^C16Claim
DTL^TEA003886004^E^MY0BCC#6485163100 641301137^+^50000^MGE^20130304^Medical Expenses (Employee clinica^^^0^^0^^0^^0^^0^^0^^0^^0^^C16Claim
HDR^TEA003886162^082792^Lim^Jia Jieh^^458^+^280400^+^280400^+^0000^+^0000^+^0000^^0^^0^^0^^0^^0^^0^20130305^^^^^^^222^MY0BD^4^jia_jieh.lim@se1.bp.com^ MY0BCC#6482363474
DTL^TEA003886162^E^MY0BCC#6482363474 641301137^+^110000^MGE^20130131^Medical Expenses (Employee clinica^^^0^^0^^0^^0^^0^^0^^0^^0^^C16Medical claim 31/1,20/2,28/2
DTL^TEA003886162^E^MY0BCC#6482363474 641301137^+^60000^MGE^20130220^Medical Expenses (Employee clinica^^^0^^0^^0^^0^^0^^0^^0^^0^^C16Medical claim 31/1,20/2,28/2
DTL^TEA003886162^E^MY0BCC#6482363474 641301137^+^50400^MGE^20130220^Medical Expenses (Employee clinica^^^0^^0^^0^^0^^0^^0^^0^^0^^C16Medical claim 31/1,20/2,28/2
DTL^TEA003886162^E^MY0BCC#6482363474 641301137^+^60000^MGE^20130228^Medical Expenses (Employee clinica^^^0^^0^^0^^0^^0^^0^^0^^0^^C16Medical claim 31/1,20/2,28/2
答案 0 :(得分:0)
你的解释和伪造的代码和示例数据似乎讲述了一个不同的故事
但是要读取第二个字段,一旦打开文件并按照描述进行排序
open(my $ex1,">EX1.txt")||die"EX1.txt $!";
open(my $ex2,">EX2.txt")||die"EX2.txt $!";
$wanted="TEA003890459";
while($line = <$ifile>) {
@field=split('\^',$line);
if ($field[1] eq $wanted) { # fields start from 0 so 1 is the second field
print $ex1 $line;
else {
print $ex2 $line;
}
}
编辑:修复拆分arg
答案 1 :(得分:0)
类似的东西:
#!/usr/bin/perl
foreach (<>) {
my @out = split(/\^/,$_);
if ($out[0] eq 'HDR') {
close OUTFILE;
open OUTFILE,">>$out[1].txt" or die();
} elsif ($out[0] eq 'DTL') {
print OUTFILE $_;
}
}
使用以下命令运行:
./split.pl < infile.txt
将拆分为每种标头类型的文件。您可以使用Linux wc命令计算每个条目的条目。