#!/usr/bin/perl # http://perlmonks.org/?node_id=1207825 use strict; use warnings; open my $file1, '<', \< }; while( <$file1> ) { my @replacements = split; print $contents2 =~ s/"PI" "\K\w+/ shift @replacements /ger; }