[英]Start reading a large file from a certain line in perl
I have following code: 我有以下代码:
open(DATA_IN, "<$in_file") or die "Couldn't open file $in_file, $!";
open(DATA_OUT, ">$out_file") or die "Couldn't open file $out_file, $!";
while(<DATA_IN>){
if($_ =~ /pattern_1/){
#extract some data
open(DATA_TEMP, "<$in_file") or die "Couldn't open file $in_file, $!";
TEMP: while(<DATA_TEMP>){
if($_ =~ /pattern_2/){
my $i = 0;
my $line;
while ($i<4){
$line = <DATA_TEMP>;
$i++;
}
print $line; #print the data 4 lines after the matched pattern_2
last TEMP;
}
}
}
}
It works fine, but the issue is that it loads $in_file everytime for pattern_1 match from the start which takes a long time. 它工作正常,但问题是,每次从一开始就为pattern_1匹配每次都加载$ in_file,这需要很长时间。 Can you suggest me a way to load $in_file only from pattern_1 onwards?
您能否建议我仅从pattern_1开始加载$ in_file的方法?
You can use the seek()
and tell()
methods to move around in the file. 您可以使用
seek()
和tell()
方法在文件中四处移动。 Something like the following: 类似于以下内容:
open(DATA_IN, "<$in_file") or die "Couldn't open file $in_file, $!";
open(DATA_OUT, ">$out_file") or die "Couldn't open file $out_file, $!";
while(<DATA_IN>){
if($_ =~ /pattern_1/){
# Save the current position
my $saved_position = tell(DATA_IN);
# extract some data
TEMP: while(<DATA_IN>){
if($_ =~ /pattern_2/){
my $i = 0;
my $line;
while ($i<4){
$line = <DATA_IN>;
$i++;
}
print $line; #print the data 4 lines after the matched pattern_2
last TEMP;
}
}
# Restore the saved position
seek(DATA_IN, saved_position, 0);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.