【发布时间】:2011-07-20 08:01:34
【问题描述】:
我有一个文本文件,我想抓取以特定模式开始并以特定模式结束的特定行。 示例:
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);
提前感谢您的帮助。
【问题讨论】:
-
我深知,当您写“无法打开日志文件。”时,您的意思是写“无法打开 $LOGFILE:$!”。
标签: perl file string-matching