3

perlを使用して基準に基づいて行を配列に分割する方法は?

各段落の間にスペースの区切り文字がある次のファイルがあります..

ログ情報.txt

               LogA
                    fjlfjdklafjdkla;fdjklafdja
                    adjalfdlafdjkla;fdla;fdl;a
                    faklfdaldflkafdlkafdklafdl
              LogB
                    djfkalfjdklafdkla;fdkla;fd
                    afdjkalfdkla;fdlk;afdla;fd
              LogC
                    djfkalfjdklafdkla;fdkla;fd
                    afdjkalfdkla;fdlk;afdla;fd


               LogA
                    fjlfjdklafjdkla;fdjklafdja
                    adjalfdlafdjkla;fdla;fdl;a
                    faklfdaldflkafdlkafdklafdl
              LogB
                    djfkalfjdklafdkla;fdkla;fd
                    afdjkalfdkla;fdlk;afdla;fd
              LogC
                    djfkalfjdklafdkla;fdkla;fd
                    afdjkalfdkla;fdlk;afdla;fd


               LogA
                    fjlfjdklafjdkla;fdjklafdja
                    adjalfdlafdjkla;fdla;fdl;a
                    faklfdaldflkafdlkafdklafdl
              LogB
                    djfkalfjdklafdkla;fdkla;fd
                    afdjkalfdkla;fdlk;afdla;fd
              LogC
                    djfkalfjdklafdkla;fdkla;fd
                    afdjkalfdkla;fdlk;afdla;fd

perl を使用して、LogB 情報のみを取得するにはどうすればよいですか

4

1 に答える 1

1
my $LogB = 0;
while (<>) {
    $LogB = 1 if /^\s*LogB\b/;        # set flag if line is LogB
    $LogB = 0 if /^\s*Log[^B]\b/;     # clear flag if some other log starts here
    print $_ if $LogB;                # print out LogB, and its entries
}

先頭の空白をトリミングして s を取り除きながらリストを作成するにはLogB:

my ($LogB, @data);
while (<>) {chomp;
    $LogB = 1 if /^\s*LogB\b/;
    $LogB = 0 if /^\s*Log[^B]\b/;
    $_ =~ s/^\s*(?:\bLogB\b)?\s*//;
    push(@data, $_) if $LogB and $_; 
}
print join("\n", @data), "\n";
于 2013-05-16T02:57:09.230 に答える