Find line containing A and then find line containing B and replace ending " by ABC"

Hello,

i am trying to create a bash script that will update file (Thunderbird filtering file) adding new value to the filtering. I will publish the script for anyone to use it so everyone can create global filters for all accounts, but i need help/suggestion regarding following:

Based on following quoted filter file content i think that to adjust one of the sections/filters (for example name=“Body contains, delete (ABC)” one, i need to find the line containing “Body contains, delete (ABC)”
and then from this line find first occurrence of “condition=” and on such line replace last character of that line " by a new phrase, for example OR (body,contains,$userinputphrase)"
where $userinputphrase will be supplied by user using for example command: read -r -p “Enter phrase” userinputphrase
and such phrase can be even complicated like: test “phrase2” $’;\/ xyz
(while i will need to convert " to \\" but that is another issue i will solve easily i think.)

The filtering file msgFilterRules.dat looks like this for example:

name="Subject contains, delete"
enabled="yes"
type="17"
action="Delete"
condition="OR (subject,contains,spam message) OR (subject,contains,spam2 message)"

name="Sender match, delete"
enabled="yes"
type="1"
action="Delete"
condition="OR (from,is,bad@sender.tld) OR (from,contains,bad@sender2.tld)"

name="Body contains, delete (ABC)"
enabled="yes"
type="1"
action="Delete"
condition="OR (body,contains,Central Bank of Nigeria) OR (body,contains,test \\"phrase\\" $';\/ abc)"

name="Subject & sender contains, delete"
enabled="yes"
type="1"
action="Delete"
condition="AND (subject,contains,badsubject) AND (from,contains,@baddomain.tld)"

Can you please help with the right sed/awk command?

No, since I can’t figure out what you’re actually asking. Why don’t you post ONE record of the input, and what you want to see on the output? Not gonna guess.

Thanks, input .dat file content i have mentioned above, output content should be like this.

The change is that this line:
condition="OR (body,contains,Central Bank of Nigeria) OR (body,contains,test \\"phrase\\" $';\/ abc)"

turned into:
condition="OR (body,contains,Central Bank of Nigeria) OR (body,contains,test \\"phrase\\" $';\/ abc) OR (body,contains,test \\"phrase2\\" $';\/ xyz)"

And within that replacement, this part:
test \\"phrase2\\" $';\/ xyz

is the result of the bash script prompt and user input of:
test "phrase2" $';\/ xyz

I know this is quite difficult, but i hope in the light of my initial post it makes more sense what i want bash script do with the file:

  1. prompts user to input phrase, he input test "phrase2" $';\/ xyz then script replace " by \\" in the user input phrase
  2. find Body contains, delete (ABC) in the file msgFilterRules.dat, then from the resulting line finds first line that contains condition= and on such line replace ending " by OR (body,contains,$userinputphrase)" where the $userinputphrase is something that user input earlier in step 1., in our case he input: test "phrase2" $';\/ xyz

My major uncertainty is how the command described a few lines above in point “2.” should look like - the command that search phrase, then from resulting line find next phrase and on this second phrase matching line do replacement of " by the phrase that contains various special chars as mentioned.

I wish you can help me, or tell me what you do not understand. If many doubts, can we PM and contact via chat please? I know this takes time, i would send some Paypal gift if wanted.