#!/usr/bin/perl use strict; my $file1 = <<END_FILE2; Iteration 1: 0: Data: 1023 1023 1: Data: 200 1023 2: Data: 300 1023 3: Data: 250 1023 4: Data: 1023 1023 Iteration 2: 0: Data: 1023 1024 1: Data: 1023 60 2: Data: 1023 90 3: Data: 1023 100 4: Data: 1023 1023 END_FILE2 my (%record, $key); for my $i (\$file1) { open( my $file_1, '<', $i ) or die "cannot open file"; while( <$file_1> ) { $key = $1 if ( s/^(Iteration \d\d?):// ); push( @{ $record{$key} }, $1 ) if ( /(\d:\s.*)/ ); } close( $file_1 ); } while (my $line = <DATA>) { if ( $line =~ s/^(Iteration \d\d?):// ) { $key = $1; print "$key:\n"; } my ($index) = $line =~ /^(\d)/; print "File1:$record{$key}->[$index] File2:$line" if ( defined( $index ) ); print "\n" if ( $index == $#{ $record{$key} } ); } __DATA__ Iteration 1: 0: Data: 1023 1023 1: Data: 250 1023 2: Data: 60 1023 3: Data: 99 1023 4: Data: 1023 1023 Iteration 2: 0: Data: 1023 1024 1: Data: 1023 60 2: Data: 1023 90 3: Data: 1023 100 4: Data: 1023 1023
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 bichonfrise74
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: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |