Perl: Search for string on line then search and replace text

Hi All,

I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text.

An example of 4 lines in my file is:

  1. MatchText_randomNumberOfText moreData ReplaceMe moreData
  2. MatchText_randomNumberOfText moreData moreData ReplaceMe moreData moreData
  3. MatchText_randomNumberOfText moreData ReplaceMe moreData moreData
  4. TextTextText_randomNumberOfText moreData ReplaceMe moreData moreData

The above is an example of four lines. I want to find all the lines containing "MatchText" (lines 1,2 and 3) and replace the "ReplaceMe" in that line with "REPLACED".

"ReplaceMe" occurs multiple times in lines I do not want to replace it in (as in line 4 above)

Hope that makes sense!!
Many thanks

loop through each line.

if($line =~ /MatchText/){
    $line =~ s/ReplaceMe/REPLACED/gi;
}
perl -pi -e 's/ReplaceMe/REPLACED/ if /MatchText/' filename

using sed:

sed -i '/MatchText/ s/ReplaceMe/REPLACED/' filename

Thanks a million, great stuff :b: