Пожалуйста, смотрите код для объяснения
#!/usr/bin/perl
#
# USAGE:
# prog.pl -a fileA -b fileB
#
# Description:
# Demonstration code for StackOverflow Q59942022
#
# Parameters:
# -a,--filea input file with pattern to substitudes
# -b,--fileb input file with values for substitution
# -d,--debug debug flag
# -h,--help brief help message
# --man manual page with more details
#
# StackOverflow:
# Question 59942022
#
# Author:
# Polar Bear
#
# Date: Tue Jan 28 3:09:00 PST 2020
#
use warnings;
use strict;
use feature 'say';
use Getopt::Long qw(GetOptions);
use Pod::Usage;
use Data::Dumper;
my $debug = 0;
my $fh; # file handler
my %opt; # command line options
my %data; # fileA data storage
my %fileb; # fileB data storage
my @order; # to keep line order
GetOptions(
'filea|a=s' => \$opt{data},
'fileb|b=s' => \$opt{fileb},
'debug|d' => \$opt{debug},
'help|?' => \$opt{help},
'man' => \$opt{man}
) or pod2usage(2);
pod2usage(1) if $opt{help};
pod2usage(-exitval => 0, -verbose => 2) if $opt{man};
print Dumper(\%opt) if $opt{debug};
open $fh, '<', $opt{data}
or die "Couldn't open $opt{data}";
map { # process fileA (pattern)
chomp;
my @a = split /\t/;
push @order, $a[0]; # preserve line order
push @{$data{$a[0]}}, @a[1..$#a]
} <$fh>;
close $fh;
open $fh, '<', $opt{fileb}
or die "Couldn't open $opt{fileb}";
map { # process fileB (values)
chomp;
my @a = split /\t/;
push @{$fileb{$a[0]}}, @a[1..$#a];
} <$fh>;
close $fh;
say Dumper(\%data) if $debug;
say Dumper(\%fileb) if $debug;
foreach my $k ( @order ) { # make required substitution
if( defined $fileb{$k} ) {
foreach my $i (0..$#{$data{$k}}) {
$data{$k}[$i] = $fileb{$k}[0] if $data{$k}[$i] eq 'A';
$data{$k}[$i] = $fileb{$k}[1] if $data{$k}[$i] eq 'B';
$data{$k}[$i] = $fileb{$k}[2] if $data{$k}[$i] eq 'X';
}
}
say join "\t", $k, @{$data{$k}}; # output result to console
}
__END__
=head1 NAME
program - describe program's functionality
=head1 SYNOPSIS
program.pl [options]
Options:
-a,--filea fileA input filename
-b,--fileb fileB input filename
-d,--debug output debug information
-?,--help brief help message
--man full documentation
=head1 OPTIONS
=over 4
=item B<-a,--filea>
FileA input filename
=item B<-b,--fileb>
FileB input filename
=item B<-d,--debug>
Print debug information.
=item B<-?,--help>
Print a brief help message and exits.
=item B<--man>
Prints the manual page and exits.
=back
B<This program> reads two input files and substitudes values in first file
according predefined rules: A - second column, B - third column, X - forth column
=cut
Входной файл A
ID item1 item2 item3 item4 ..
592 A B X B
598 A B X A
612 A X A X
650 A X A B
700 A X A B
822 A X A A
830 A X A A
840 A X A X
Входной файлB
ID var1 var2 var3
568 G A NA
570 T C NA
592 T G NA
598 A T NA
612 C A NA
650 C T NA
700 T C NA
822 T C NA
830 T A NA
840 G C NA
900 T G NA
1000 A T NA
Выход
ID item1 item2 item3 item4 ..
592 T G NA G
598 A T NA A
612 C NA C NA
650 C NA C T
700 T NA T C
822 T NA T T
830 T NA T T
840 G NA G NA