#!/usr/bin/perl $filename=$ARGV[0]; open FIL,$filename; @fil=; $fil=join '',@fil; sub replace { $w=shift; $p=shift; if ($hash{$p}++) { return $p; } else { return $w; } } $fil =~ s/(\\emph\{((.|\n)+?)\})/replace($1,$2);/ge; print $fil;