use warnings; use strict; my ($fname) = @ARGV; open my $fh, '<', $fname or die $!; my %cit_ids; # Fill the hash with relevant ids while (<$fh>) { while (m//g) { $cit_ids{$+{id}} = $+{name} . $+{num}; } } seek $fh, 0, 0; # Rewind the file while (<$fh>) { s/rid="(b0*\d+)"/rid="$cit_ids{$1}"/g; print; }