我有以下文件:
some data
some data 1
some data 1
hostname = abc
some data
some data 1
some data 1
some data
some data 1
some data 1
hostname = xyz
some data
some data 1
some data 1
some data
some data 1
some data 1
hostname = pqr
some data
some data 1
some data 1
some data
some data 1
some data 1
如何使用Perl提取两个主机名行条目之间的行?
答案 0 :(得分:1)
怎么样:
#!perl -w
my @someArray;
while (<DATA>) {
if (/hostname.*/) {
print @someArray;
@someArray = ();
}
else {
push (@someArray, $_);
}
}
__DATA__
some data
some data 1
some data 1
hostname = abc
some data
some data 1
some data 1
some data
some data 1
some data 1
hostname = xyz
some data
some data 1
some data 1
答案 1 :(得分:0)
perl -nle'/hostname = abc/../hostname = xyz/&&print' datafile
答案 2 :(得分:0)
将整个事物拖入字符串$string
,然后执行以下操作:
$string =~ /hostname = abc\n(.*)hostname = xyz\n/s;
abc
和xyz
主机名行之间的行将在$1
之后。{/ p>
答案 3 :(得分:0)
split
确实足够了,只要给它正确的模式。
use DDS; Dump split /hostname = .*\n/, 'some data
some data 1
some data 1
hostname = abc
some data
some data 1
some data 1
some data
some data 1
some data 1
hostname = xyz
some data
some data 1
some data 1
some data
some data 1
some data 1
hostname = pqr
some data
some data 1
some data 1
some data
some data 1
some data 1';
输出
$VAR1 = "some data\nsome data 1\nsome data 1\n";
$VAR2 = "some data\nsome data 1\nsome data 1\nsome data\nsome data 1\nsome data 1\n";
$VAR3 = "some data\nsome data 1\nsome data 1\nsome data\nsome data 1\nsome data 1\n";
$VAR4 = "some data\nsome data 1\nsome data 1\nsome data\nsome data 1\nsome data 1";