我有一个文本文件,我想抓取以模式开头并以特定模式结尾的特定行。
例子:
Text
Text
Startpattern
print this line
Print this line
print this line
Endpattern
Text
Text
Text
还应打印起始图案和结束图案。我的第一次尝试并没有真正成功:
my $LOGFILE = "/var/log/logfile";
my @array;
# open the file (or die trying)
open(LOGFILE) or die("Could not open log file.");
foreach $line () {
if($line =~ m/Sstartpattern/i){
print $line;
foreach $line2 () {
if(!$line =~ m/Endpattern/i){
print $line2;
}
}
}
}
close(LOGFILE);
在此先感谢您的帮助。
最佳答案
您可以使用标量 range operator :
open my $fh, "<", $file or die $!;
while (<$fh>) {
print if /Startpattern/ .. /Endpattern/;
}
关于perl 读取文件并抓取特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5389115/