我有兩個非常大的XML文件,它們有不同的行結尾。 文件A在每個XML記錄的末尾有CR LF。文件B在每個XML記錄的末尾只有CR。如何使用不同的行分隔符讀取大文件?
爲了正確讀取文件B,我需要將內置Perl變量$ /設置爲「\ r」。 但是,如果我使用與文件A相同的腳本,腳本不會讀取文件中的每一行,而是將其作爲單行讀取。
如何使腳本與具有各種行結束分隔符的文本文件兼容?在下面的代碼中,腳本正在讀取XML數據,然後使用正則表達式根據特定XML標記記錄結束標記(如< \ record>)拆分記錄。最後它將請求的記錄寫入文件。
open my $file_handle, '+<', $inputFile or die $!;
local $/ = "\r";
while(my $line = <$file_handle>) { #read file line-by-line. Does not load whole file into memory.
$current_line = $line;
if ($spliceAmount > $recordCounter) { #if the splice amount hasn't been reached yet
push (@setofRecords,$current_line); #start adding each line to the set of records array
if ($current_line =~ m|$recordSeparator|) { #check for the node to splice on
$recordCounter ++; #if the record separator was found (end of that record) then increment the record counter
}
}
#don't close the file because we need to read the last line
}
$current_line =~/(\<\/\w+\>$)/;
$endTag = $1;
print "\n\n";
print "End Tag: $endTag \n\n";
close $file_handle;
由於您認爲XML文件在合理的位置甚至存在換行符,您將受到懲罰。 –
這意味着要分發,所以我不想用模塊來解決這個問題。這是否意味着我不得不重新編寫Perl以外的其他語言,以便更好地支持XML解析? – astra