1717
1818my $col_f1 =$options -> {' c1' };
1919my $col_f2 =$options -> {' c2' };
20+ $options -> {' hdr' }=0 unless ($options -> {' hdr' });
2021chomp $col_f1 ;
2122chomp $col_f2 ;
2223
2526
2627# create hash of both the files....
2728
28- my ($file1_hash )=get_file_hash($fh1 ,$col_f1 ,$options -> {' h ' },$f1 );
29- my ($file2_hash )=get_file_hash($fh2 ,$col_f2 ,$options -> {' h ' },$f2 );
29+ my ($file1_hash )=get_file_hash($fh1 ,$col_f1 ,$options -> {' hdr ' },$f1 );
30+ my ($file2_hash )=get_file_hash($fh2 ,$col_f2 ,$options -> {' hdr ' },$f2 );
3031
3132# combine files
32- combine_hash($file1_hash ,$file2_hash ,$fh_common ,$options -> {' h ' });
33+ combine_hash($file1_hash ,$file2_hash ,$fh_common ,$options -> {' hdr ' });
3334
3435# sub to combine file data...
3536sub combine_hash {
3637 my ($hash_f1 ,$hash_f2 ,$fh_combined ,$header )=@_ ;
3738 my $count =0;
38- # Write common rows file...
39+ # Write header lines to file...
3940 if ($header ){
41+ print $fh_common $hash_f1 -> {' pre_header_lines' };
4042 print $fh_common $hash_f1 -> {' header' }." \t " .$hash_f2 -> {' header' }." \n " ;
4143 }
4244 # print Dumper $hash_f1;
45+ # write common rows
4346 foreach my $key (keys %$hash_f1 ) {
4447 next if $key eq ' header' ;
4548 if ($hash_f2 -> {$key }) {
@@ -52,16 +55,22 @@ sub combine_hash {
5255
5356# create hash from file
5457sub get_file_hash {
55- my ($fh ,$columns ,$header ,$f )=@_ ;
58+ my ($fh ,$columns ,$header_row ,$f )=@_ ;
5659 my $file_hash ;
5760 my $count =0;
5861 while (my $line =<$fh >) {
5962 $count ++;
63+ next if $line =~/ ^\s +\n / ;
6064 $line =~s /\n // g ;
61- my ($col_key ) = get_col_key($line ,$columns );
62- if ($header && $count == 1) {
63- $file_hash -> {' header' }=" $line " ;
64- print " Columns to compare from file: $f : $col_key \n " ;
65+ my ($col_key ) = get_col_key($line ,$columns ) if $count >= $header_row ;
66+ if ($count <= $header_row ) {
67+ if ($count ==$header_row ) {
68+ $file_hash -> {' header' }=$line ;
69+ }
70+ else {
71+ $file_hash -> {' pre_header_lines' }.=" $line \n " ;
72+ }
73+ print " Columns to compare from file: : $col_key \n " if defined $col_key ;
6574 next ;
6675 }
6776 $file_hash -> {$col_key }=" $line " ;
@@ -77,6 +86,7 @@ sub get_file_hash {
7786sub get_col_key {
7887 my ($line ,$columns )=@_ ;
7988 my $col_key ;
89+
8090 my @col =split (' ,' ,$columns );
8191 foreach my $col_num (@col ) {
8292 my ($field )=(split " \t " , $line )[$col_num - 1];
@@ -103,7 +113,7 @@ sub option_builder {
103113 ' f2|file2=s' => \$opts {' f2' },
104114 ' c1|col1=s' => \$opts {' c1' },
105115 ' c2|col2=s' => \$opts {' c2' },
106- ' h |header=s' => \$opts {' h ' },
116+ ' hdr |header=s' => \$opts {' hdr ' },
107117 ' v|version' => \$opts {' v' },
108118 );
109119
@@ -115,21 +125,20 @@ sub option_builder {
115125__END__
116126=head1 NAME
117127
118- compare_files .pl - creates file with matching columns from both files
128+ merge2files .pl - creates a single merged file using user defined matching columns from both files
119129
120130=head1 SYNOPSIS
121131
122- createConfig .pl [-h] -p -o [ -o -h -v ]
132+ merge2files .pl [-h] -f1 -f2 -c1 -c2 [-hdr ]
123133
124- Required Options (project must be defined) :
134+ Required Options:
125135 --help (-h) This message and format of input file
126136 One or more of the following:
127-
128137 --file1 (-f1) file1
129138 --file2 (-f2) file2
130139 --col1 (-c1) comma separated column numbers fom file1
131140 --col2 (-c2) comma separated columns numbers fom file2
132- --header (-h) [1|0] header line present
141+ --header (-hdr) header row number
133142
134143=cut
135144
0 commit comments