使用Perl在重复标题下收集文本

时间:2013-05-24 22:47:20

标签: perl parsing lines

New Features: 
- Plumbing for DSI dynamic refresh rate change
    - jdflksjdfksjdkfjsdf
    -jdksjfdkjfskjdfskf
Fixes: 
-  Fix KW issue 7449.
- Adding fix to avoid reading EDIDPO twice on each HPD,  EDID cache          
-   Mark layer unused to allow the test to run.

    Fixed CRs: 3847263, 498327498, 92834927

上面的文字在我的文本文件中重复了几次。我想解析每个事件并提取数据以放入下一个文本文件,该文件将列出“新功能”下的所有新功能,“修复”下的修复和“固定CR”下修复的CR。

如何进一步改进我的代码以使其工作?此时输出仅显示标题下的新功能。换句话说,我想重新使用我的子程序myparser()来获取“修复:”和“固定CR:”和“固定CR:”和行尾之间的界限 输出:

New Features: 
  -Plumbing for DSI dynamic refresh rate change
  -add watchdog support for 8084 and 8x62
  -Displays supported and the features supported for each of them has been made   chip-specific.
  -Size optimization for eventlog
  -Integrate new power framework from the development sandbox
  -New KMD CAP: supportsDummyPageMapping
  -Adding support for chipID GfxLibChipIDOxili305Dino
  -Using parent driver to handle thermal mitigation request instead of AV stream      

以下是我的代码:

#!perl -w
use strict;
use autodie;
use warnings;

open (FILE_IN,"<S2.txt") or print "Failed to open S2.txt\n" and die;
open FILE_OUT,"+>ramu_15.txt" or print "Failed to open S2.txt\n" and die;

my $a = "New Features:";
my $b = "Fixes:";
my $c = "Fixed CRs:";

sub myparser () {

    my $started       = 0;
    my $printFeatures = 1;

    print "START....\n";
    my @lines = <FILE_IN>;

    foreach (@lines) {

        if ($_ =~ /$a/) {
            if ($printFeatures == 1) {
                print FILE_OUT $_;
                $printFeatures = 0;
            }
            $started = 1;
            next;
        }
        if ($started == 1) {
            if ($_ !~ /$b/) {
                print "$_\n" if $_ ne "\n";
                print FILE_OUT $_ if $_ ne "\n";
            }
            else {
                $started = 0;
            }
        }
    }
}

myparser();
print "...END\n";

close FILE_IN;
close FILE_OUT;

1 个答案:

答案 0 :(得分:1)

显而易见的解决方案是使用哈希。

要测试一些有代表性的数据会有所帮助,但这个程序应该按照你的要求进行。

use strict;
use warnings;
use autodie;

open my $in, '<', 'S2.txt';
open my $out, '>', 'ramu_15.txt';

my %data;
my $key;

while (my $text = <$in>) {
  chomp $text;
  if (  $text =~ /(\w+(?:\s+\w+)*):\s*(.*)/  ) {
    $key = $1;
    $text = $2;
  }
  push @{$data{$key}}, $text if $key and $text =~ /\S/;
}

for ('New Features', 'Fixes', 'Fixed CRs') {
  print "\n$_\n";
  print "$_\n" for @{$data{$_}};
}

<强>输出

New Features
- Plumbing for DSI dynamic refresh rate change
    - jdflksjdfksjdkfjsdf
    -jdksjfdkjfskjdfskf

Fixes
-  Fix KW issue 7449.
- Adding fix to avoid reading EDIDPO twice on each HPD,  EDID cache          
-   Mark layer unused to allow the test to run.

Fixed CRs
3847263, 498327498, 92834927