I think the problem might be solved by the code below for any number of su and mu files. It doesn't check if every file has the same number of requests, (would need to add some test). Also, the glob function assumes all the files are in the current working directory. If that's not the case, then a path would need to be prepended to the glob expression. I used Sort::Naturally to sort the respective files saved in the arrays and List::Util for the sum function.

Chris

#!/usr/bin/perl use strict; use warnings; use Sort::Naturally; use List::Util 'sum'; use List::Compare; my %data; my %file_requests; my $base; my @su_files = nsort glob "su[1-9]*_sdkperfmonlog.txt"; my @mu_files = nsort glob "mu[1-9]*_sdkperfmonlog.txt"; for my $file (@su_files, @mu_files) { my $request; open my $fh, "<", $file or die "Unable to open $file for reading. +$!"; while (<$fh>) { if (/Start\s+-+\s+\d+:\d+:(.+)/) { $request = $1; } elsif (/ProcessRequest\t([.\d]+)/) { $data{$request}{$file} = $1; push @{ $file_requests{$file} }, $request; } } close $fh or die "Unable to close $file. $!"; # validate $base ||= $file_requests{$file}; my $lc = List::Compare->new( $base, $file_requests{$file} ); die "Non-matching requests from $file. $!" unless $lc->is_LequivalentR; } # Header print join("\t", 'Request Name', (map {/(su\d+)_/} @su_files), 'Su-Avg', (map {/(mu\d+)_/} @mu_files), 'Mu-Avg' ), "\n"; # Body for my $request (keys %data) { print $request, "\t"; my @vals = @{ $data{$request} }{ @su_files }; print join("\t", @vals, sprintf "%0.3f", sum(@vals)/@vals), "\t"; @vals = @{ $data{$request} }{ @mu_files }; print join("\t", @vals, sprintf "%0.3f", sum(@vals)/@vals),"\n"; }

Update: Added validation code


In reply to Re: Parsing many files and output to one file. Pls HELP by Cristoforo
in thread Parsing many files and output to one file. Pls HELP by hiradhu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.