perl -MPath::Tiny -pe 'BEGIN{m/_id = (\d+) id = (\S+)/ and $id{$1}=$2 for path("contacts.txt")->lines} s/handle_id = (\d+)/$id{$1}/' messages.txt