-1

次のようなテキストファイルがあります:

Name : first  
File name : first_1  
Year : 12  
etc  
etc  
etc  
T1 : this is t1  
T2 : this is t2  
T3 : this is t3  
Conclusion : Success  
Name : first  
File name : first_2   
Year : 13  
etc  
etc  
etc  
T1 : this is t1  
T2 : this is t2  
T3 : this is t3  
Conclusion : Success 
Name : second  
File name : second_1  
Year : 12  
etc  
etc  
etc  
T1 : this is t1  
T2 : this is t2  
T3 : this is t3  
Conclusion : Failure  
Name : first  
File name : first_3  
Year : 12  
etc  
etc  
etc  
T1 : this is t1  
T2 : this is t2  
T3 : this is t3  
Conclusion : Success 

等々.....

次の出力が得られる perl スクリプトが必要です。

Naming  File_name  Year  Conclusion  Reason  
first   first_1    12    Success     this is t1, this is t2, this is t3    
first   first_2    13    Success     this is t1, this is t2, this is t3   
second  second_1   12    Failure     this is t1, this is t2, this is t3  
first   first_3    12    Success     this is t1, this is t2, this is t3 
4

2 に答える 2

1

以下は、必要なことを行います。解析するファイルを STDIN から読み取ります。他の行の配列である可能性があります。

#!/usr/bin/perl

use strict;

my $Naming = "";
my $File_name = "";
my $Year = "";
my @Reason = ();
my $Conclusion = "";

print "Naming\tFile_name\tYear\tConclusion\tReason\n";
while (my $line = <STDIN>) {
  $line =~ /Conclusion : (\w+)/ and do {
    $Conclusion = $1;

    print "$Naming\t$File_name\t$Year\t$Conclusion\t" . join(", ",@Reason) . "\n";
    $Naming = "";
    $File_name = "";
    $Year = "";
    @Reason = ();
    $Conclusion = "";
  };

  $line =~ /Name : (.*)/ and $Naming = $1;
  $line =~ /File name : (.*)/ and $File_name = $1;
  $line =~ /Year : (\d+)/ and $Year = $1;
  $line =~ /T\d : (.*)/ and push(@Reason,$1);
}
于 2013-03-18T08:19:11.600 に答える
0

何に問題があるかを特定しませんでした。ストリームをレコードに分割しますか? これはトリックを行います:

my @buf;
while (<>) {
   if (@buf && /^Name :/) {
      process_record(@buf);
      @buf = ();
   }

   push @buf, $_;
}

process_record(@buf) if @buf;
于 2013-03-18T08:16:39.923 に答える