#!/usr/bin/perl use strict; use warnings; my %hash = (); open my $fh, '<', 'sample_name_ID_hash.txt' or die "Cant open file $!"; LINE: while (my $line = <$fh>) { my ($key, $value) = split /\s/, $line; next LINE if not $key; $hash{$key} = $value; chomp (%hash); } close $fh; @file_array = <*.nex> or die $!; #find all the files foreach $file (@file_array) { my $prefix = $file; $prefix =~ s/\.nex//; open FH, "$file", or die $!; print "found $file with prefix $prefix\n"; open OUT, ">out$prefix.nex" or die $!; while () { if (/^\s+\'(\S+):(\S\d+)\|\w+\|(\d)\S+\'(.*)\n/) { $var1 = "$1"; $var2 = "$2"; $var3 = "$3"; $var4 = "$4"; print OUT "'$hash{$var1}_${var2}_$var3'$var4\n"; } elsif (/(.*)\n/) { print OUT "$1\n"; } } }