It appears to me that your datafiles have a hierarchical structure. So you'll have to parse them, and I think, for a generic parsing routine (now you only have to merge the files, but I'm sure that later on you'll have to write a similar script to do other stuff to the same files), it makes the most sense to parse the data into a data structure (a tree); see perldsc and perllol to see what kind of stuff I'm talking about.

Load all datafiles in a proper manner into the same data structure.

As a second step, you'll have to produce the desired output from the produced tree.

Step 1: load the data from all files into a data structure:

my @files = qw(File1.txt File2.txt); my %tree; my(@iterations, @names); # order foreach my $file (qw(File1.txt File2.txt)) { open my $fh, '<', $file or die "Cannot open file $file: $!"; (my $name = $file) =~ s/\.\w+$//; # remove extension push @refs, $ref; # order my($iteration); while(<$fh>) { chomp; if(/^Iteration /) { $iteration = $_; push @iterations, $iteration unless $tree{$iteration}; # +key order } elsif(my($i) = /^(\d+):/) { $tree{$iteration}[$i]{$name} = $_; } } }

You can show the contents of the data structure, to see if it works:

use Data::Dumper; print Dumper \%tree;

Step 1 is done. Now step 2: print out the data to a file.

foreach my $iteration (@iterations) { print "$iteration\n"; # section header my $section = $tree{$iteration}; # array ref for my $i (0 .. $#$section) { my @data; foreach my $name (@names) { my $data = $section->[$i]{$name}; next unless defined $data; push @data, "$name:$data"; } if(@data) { my $line = join " ", @data; print "$line\n"; } } }

I think that rounds it up...


In reply to Re: How do I Extract contents from given input files and merge into one text file based on Unique keys present in input files by bart
in thread How do I Extract contents from given input files and merge into one text file based on Unique keys present in input files by Anonymous Monk

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.