如何使用Perl从文件中选择行

时间:2013-11-15 12:05:22

标签: perl

我正在尝试检查文件中的每一行是否有字符串,如果有该字符串则打印该行。

数组示例:

---
BIP1288I: Message flow 'flow1' on execution group 'EG1' is running.

Additional thread instances: '0'
Deployed: '11/12/13 1:54 AM' in Bar file '/MSD/deploy/ENV/EG1/flow1.bar'
Last edited: '5/24/13 4:38 PM'.
Long description: ''
User-defined property names:
Keywords:

---
BIP1288I: Message flow 'flow2' on execution group 'EG1' is running.

Additional thread instances: '0'
Deployed: '11/12/13 1:54 AM' in Bar file '/MSD/deploy/ENV/EG1/flow2.bar'
Last edited: '5/24/13 4:38 PM'.
Long description: ''
Keywords:

----
BIP1288I: Message flow 'flow3' on execution group 'EG1' is running.

Additional thread instances: '0'
Deployed: '11/12/13 1:54 AM' in Bar file '/MSD/deploy/ENV/EG1/flow3.bar'
Last edited: '5/24/13 4:38 PM'.
Long description: ''
User-defined property names:
  'DBSchema' = 'SIDBT01'
  'LogLevel' = 'ERROR'
Keywords:

我正在检查此文件的每一行以检查BIP1288I和流名称'flow1'。我希望我的输出只包含这个

Additional thread instances: '0'
Deployed: '11/12/13 1:54 AM' in Bar file '/MSD/deploy/ENV/EG1/flow1.bar'
Last edited: '5/24/13 4:38 PM'.

这是我的Perl代码

foreach $line (@flows) {
  next if /^(\s)*$/;
  if ($line =~ "BIP1288I" && $line =~ "flow1") {
    $msg = "Flow found\n$line\n";
print "$msg";
  }}

4 个答案:

答案 0 :(得分:1)

我不清楚你是否真的需要使用perl,但这对于awk非常简单:

awk '/BIP1288I/ && /flow1/ { split($0, a, "\n"); print a[4],a[5],a[6]}' \
    RS=--- OFS=\\n input-file

您可以使用a2pperl中生成类似的脚本。 (a2p是perl翻译的awk。perl主要是基于awk建模。永远不要忘记你的根。)或者:

perl -wnE 'BEGIN{ $/="---"; $,="\n"}; 
    @a=split("\n"); say $a[3],$a[4],$a[5] if (m/BIP1288I/ && m/flow1/)' input-file

这不是一个严格的测试,因为如果字符串出现在记录中的任何位置它会起作用,但它应该足够了。您可能需要匹配BIP1288I.*flow1或将匹配限制在第一行,具体取决于您的要求。

答案 1 :(得分:0)

我很不清楚你的意思,但也许grep可以帮助你:

grep "pattern1" your_file | grep "pattern2"

将打印包含BOTH pattern1和pattern2

的行
egrep "pattern1|pattern2" your_file

将打印包含任何一种模式的行。

grep -v "pattern" your_file

将打印与图案不匹配的行。

所以也许你可以使用像:

这样的东西
egrep "^Additional|flow1|^Last" your_file

答案 2 :(得分:0)

我假设数组@flows包含您发布数据的所有行。

use warnings; #always use these modules!
use strict; #always use these modules!

my $flag=0; #a flag to track if we found BIP1288I & flow1 in order to print the lines that follow
for my $line (@flows) {
    if ($flag==1) {
        if ($line=~/^(?:Additional thread|Deployed)/) {
            print $line; #we print the line only if flag is 1 and the line start with the words "Additional thread" or "Deployed" or "Last edited"
        }
        elsif ($line=~/^Last edited/) {
            print $line; #we print the line only if flag is 1 and the line start with the words "Additional thread" or "Deployed" or "Last edited" 
            $flag=0; #we reset the flag
        }
    }
    if ($line=~/BIP1288I: Message flow 'flow1'/) { #this the flow we are interested in
        $flag=1; #set the flag to 1 so we can start printing the lines that will follow
    }
}

这将打印:

Additional thread instances: '0'
Deployed: '11/12/13 1:54 AM' in Bar file '/MSD/deploy/ENV/EG1/flow1.bar'
Last edited: '5/24/13 4:38 PM'.

答案 3 :(得分:0)

也许以下内容会有所帮助:

use strict;
use warnings;

local $/ = '---';

while (<>) {
    print "$1$2$3\n"
      if /BIP1288I:.+'flow1'.+\n(Addi[^\n]+\n)(Depl[^\n]+\n)(Last[^\n]+\n)/s;
}

用法:perl script.pl inFile [>outFile]

最后一个可选参数将输出定向到文件。

数据集输出:

Additional thread instances: '0'
Deployed: '11/12/13 1:54 AM' in Bar file '/MSD/deploy/ENV/EG1/flow1.bar'
Last edited: '5/24/13 4:38 PM'

假设“---”分隔记录,您可以将Perl的记录分隔符($/)设置为“---”,这样您就可以读取这些记录的块。然后你可以匹配“BIP1288I”和流程名称“flow1”并捕获你想要的其他信息。

希望这有帮助!