Similar to the idea given by bart and dwm042 above but a slightly different implementation using a hash of hashes with multiple values per key.

#!/usr/bin/perl -w use strict; my @list_of_files = qw(file1.txt file2.txt); my %data; my $file_num = 0; foreach my $file_name(@list_of_files){ $file_num++; my $file = 'File'.$file_num; my $iter; my $flag = 0; open FH, $file_name or die "Cannot open $file_name. $!"; while(<FH>){ chomp; if($_ =~ /(Iteration.+)/){ $iter = $1; $flag = 1; next; } if(/^$/){ $flag = 0; } if($flag){ push( @{$data{$iter}{$file}}, $_ ); } } close FH; } foreach my $iter ( sort keys %data ) { print "$iter\n"; my $size; my $index = 0; do { for my $file ( sort keys %{ $data{$iter} } ) { print "$file: ", @{$data{$iter}{$file}}[$index], " "; $size = $#{$data{$iter}{$file}}; } $index++; print "\n"; } while($index<=$size); print "\n"; }

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 arun_kom
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.