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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |