0
$Subj .= "HAD PROBLEMS";

if ($To) {
    $Cc = "abc\@xyz.com";
}
else {
    $Cc = "abc\@xyz.com";
}

I have the above text in a file. I need to replace the email id in $Cc with a new email id.

I have the following sed command to do so.

sed '\|HAD PROBLEMS|,/}/ s/$Cc = (\"[A-Za-z0-9])(.)([A-Za-z0-9]*)\@xyz.com\"/\$Cc = "new email\@xyz.com"/' test.txt

This command will replace the email id only for the $Cc variable in the if {} block (first occurrence of $Cc) since my ending match pattern is a '}'. I want to replace email id in the second $Cc as well. how do i match on the 2nd occurrence of '}' ?

  • 1
    Is this the only text in the file? – Kusalananda Jan 3 '17 at 8:40
0

if you want to replace the Cc address to new email address, then try the below

$ sed 's/\$Cc.*/\$Cc="new_mail\@abc.com"/g' input.txt
$Subj .= "HAD PROBLEMS";

if ($To) {
    $Cc="new_mail@abc.com"
}
else {
    $Cc="new_mail@abc.com"
}
  • 1
    I wonder if the @ in the new address should be escaped, as it is in the old one? And the g modifier on the s sed command is not needed, IMHO. – Kusalananda Jan 3 '17 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.