在Perl中提取两个关键词之间的段落

时间:2015-01-11 07:36:58

标签: perl

我是Perl的初学者,我正在尝试在搜索关键词后在两个关键词之间打印段落。

因此,这应该搜索ooaijp01.intlb.net-pool,如果匹配,则应在关键字之间打印段落

以下是输入文件。你能帮助我实现这个目标吗? 它应该忽略区分大小写

,文件名为gslb_data

pool {
   name           "ooaijp01.intlb.net-pool"
   ttl            *
   preferred      ***
   alternatkke      **
   fallback       *****
   fallback_ipv4  ****
   partition "Common"

   member         *****
   member         *******
}

pool


pool {
   name           "ooaijj01.intlb.net-pool"
   ttl            *
   preferred      ***
   alternatkke      **
   fallback       *****
   fallback_ipv4  ****
   partition "Common"

   member         *****
   member         *******
}

pool


pool {
   name           "ooaijk01.intlb.net-pool"
   ttl            *
   preferred      ***
   alternatkke      **
   fallback       *****
   fallback_ipv4  ****
   partition "Common"

   member         *****
   member         *******
}

pool

我已尝试使用此代码,但它无效。

#!/usr/bin/perl

open my $fh, '<', 'file_to_check'
    or die "Cannot open: $!";    # file_to_check is my file name
my @file = <$fh>;
close $fh;

for (0 .. $#file) {

  if ($file[$_] =~ /"ooaijp01.intlb.net-pool"/) { # is my key word ooaijp01.intlb.net-pool

    my $start = $_;

    while ($start >= 0 && $file[$start] !~ /pool/) {
      $start--;
    }

    my $end = $_;

    while ($end <= $#file && $file[$end] !~ /pool/) {
      $end++;
    }

    print "\n@file[$start+1..$end-1]";
  }
}

我的预期输出应为

pool {
   name           "ooaijp01.intlb.net-pool"
   ttl            *
   preferred      ***
   alternatkke      **
   fallback       *****
   fallback_ipv4  ****
   partition "Common"

   member         *****
   member         *******
}

pool

我希望保存在单独的输出文件中

3 个答案:

答案 0 :(得分:0)

@clement:你可以尝试下面的perl代码:

没有啜食:

use strict;
use warnings;

my $filename = $ARGV[0];
my ($file,$ext)=$filename =~ /(.*?)(\..*?)/;
my $outfile = "$file" . "\.out";
open my $fh,'<',$filename or die "File $filename cannot be opened: $!";
open my $oh,'>',$outfile or die "File $outfile cannot be opened : $!";
my $start=0;
while(<$fh>)
{
     if($_ =~ /ooaijp01\.intlb\.net\-pool/is)
      { 
         $start = $.;
     print $_ . "\n";
      }

     elsif($start>0 && $_ !~ /pool/)
      {
          print $_ . "\n";
        }

}

使用Slurping:

use strict;
use warnings;

my $filename = $ARGV[0];
my ($file,$ext)=$filename =~ /(.*?)(\..*?)/;
my $outfile = "$file" . "\.out";
open my $fh,'<',$filename or die "File $filename cannot be opened: $!";
open my $oh,'>',$outfile or die "File $outfile cannot be opened : $!";

my $document = do {
         local $/ = undef;
         open my $fh,'<',$filename or die "Couldn't open the file $filename:$!";
       <$fh>;
      };


sub Processing
{
    my $result = $_[0];
     if($result =~ /ooaijp01\.intlb\.net\-pool/isg)
       {
          print $oh $result . "\n";
        }
     }          

$document =~ s/(pool\s*\{.*?\s*\}\s*pool)/Processing($1)/eisg;    #Between the keywords pool

答案 1 :(得分:0)

这是一种完成工作的方法:

#!/usr/bin/perl
use Modern::Perl;

my $toprint;
my @out;
while(<DATA>) {
   chomp;
   if (/^\s*pool\b/) { # ^\s* is mandatory to not catch net-pool
      if ($toprint) {
         say $_ for @out;
      }
      $toprint = 0;
      @out = ($_);
   } elsif ($_ =~ /\booaijp01\.intlb\.net-pool\b/) {
      $toprint = 1;
      push @out, $_;
   } else {
      push @out, $_;
   }
}

if ($toprint) {
   say $_ for @out;
}

__DATA__
pool {
   name           "ooaijp01.intlb.net-pool"
   ttl            *
   preferred      ***
   member         *******
}

pool {
   name           "ooaijp01.intlb.net-pool"
   ttl            *
   preferred      ***
   member         *******
}

pool {
   name           "ooaijp01.xxxx"
   ttl            *
   preferred      ***
   member         *******
}

pool {
   name           "ooaijp01.intlb.net-pool"
   ttl            *
   preferred      *
   member         *
}

<强>输出:

pool {
   name           "ooaijp01.intlb.net-pool"
   ttl            *
   preferred      ***
   member         *******
}

pool {
   name           "ooaijp01.intlb.net-pool"
   ttl            *
   preferred      ***
   member         *******
}

pool {
   name           "ooaijp01.intlb.net-pool"
   ttl            *
   preferred      *
   member         *
}

答案 2 :(得分:0)

没有人提到过&#39;范围运营商&#39;了吗?

这样的事情:

print "pool {\n";
while ( <DATA> ) {
    print if /ooaijp01.intlb.net-pool/ .. /\}/;
}

将在开始和结束模式之间打印任何行。