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
  • 1
    Is this the only text in the file?
    – Kusalananda
    Jan 3, 2017 at 8:40

1 Answer 1

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="[email protected]"
}
else {
    $Cc="[email protected]"
}
1
  • 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, 2017 at 8:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .