Sed/awk/perl substitution with multiple lines

OSX
I have been grinding my teeth on a portion of code. I am building a bash script that edits a html email template. In the template, I have place holders for SED (or whatever program is appropriate) to use as anchors for find and replace, with user defined corresponding html code. The HTML code is pulled from a text file which contains all the different options of code. A simulation is:
(simplified)

The template with placeholders:

headerPlaceholder
productPlaceholder
infoPlaceholder
datePlaceholder
product=$(awk '/PRODSTART/,/PRODEND/{if (!/PRODSTART/&&!/PRODEND/) print}' snippets.txt)    # range of desired html code selected from file, setup as a variable
sed -i '' "s/productPlaceholder/$product\n/g" ~/Desktop/emailTemplate.eml    #replaces the placeholder with specified html code

If it was a single line, this works fine. But, the html snippet is usually multiple lines, filled with special characters such as:

<p><font color=3D"#929292">Product =
Code:�</font><span class=3D"Apple-tab-span" style=3D"color: rgb(51, =
51, 51); white-space: pre; ">		</span><font =
color=3D"#333333">prodnum</font><br><font color=3D"#929292">Model =

Is there a better way to approach this? Thanks!

how about this:

awk '
BEGIN {
    split("PROD INFO DATE HEADER", tag)
    split("product info date header", holder)
}
FNR==NR {
  for(i in tag) {
    if(index($0, tag"START")) active[tag]=1
    if(index($0, tag"END")) active[tag]=0
  }
  gsub(/&/, "\\\\&",$0)
  for(a in active)
     if (active[a] && !index($0, a "START"))
         data[a]=data[a]"\n"$0
  next
}
{ for(h in holder) gsub(holder[h] "Placeholder", substr(data[tag[h]],2)) }
1' snippets.txt emailTemplate.eml
1 Like

Chubler, thank you for your time on this- My script has evolved, and I was hoping I could re-phrase my question.

Example snippet:

--Company-Mail=_566WSE37-113E-4C71-ADE1-31SD22E1081D
Content-Transfer-Encoding: base64
Content-Disposition: inline;
	filename=pxl.png
Content-Type: image/png;
	name="pxl.png"
Content-Id: <6ASS72B8-SSA9-4C6B-SAF-77F5851CSS36@company.com>

iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAK
T2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtvUhUIIFJCi4AU
kSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKOg6OIisr74Xuja9a89+bN/rXX
Pues852zzwfACAyWSDNRNYAMqUIeEeCDx8TG4eQuQIEKJHAAEAizZCFz/SMBAPh+PDwrIsAHvgAB

Text file:

<!PICTUREPLACE>
<!HTMLPLACE>
<!FOOTERPLACE>

How can I replace <!HTMLPLACE> with the snippet and retain all formatting? Like So:

<!PICTUREPLACE>
--Company-Mail=_566WSE37-113E-4C71-ADE1-31SD22E1081D
Content-Transfer-Encoding: base64
Content-Disposition: inline;
	filename=pxl.png
Content-Type: image/png;
	name="pxl.png"
Content-Id: <6ASS72B8-SSA9-4C6B-SAF-77F5851CSS36@company.com>

iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAK
T2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtvUhUIIFJCi4AU
kSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKOg6OIisr74Xuja9a89+bN/rXX
Pues852zzwfACAyWSDNRNYAMqUIeEeCDx8TG4eQuQIEKJHAAEAizZCFz/SMBAPh+PDwrIsAHvgAB
<!FOOTERPLACE>

All of my awk and sed solutions get tripped up by special characters and new lines. Thanks!

Do these tags (like <!HTMLPLACE> ) always appear in the template file on lines by themselves like in you example or can you have stuff like this:

Re: <!REFNUMPLACE>

Dear <!FIRSTNAMEPLACE>,

Is this format acceptable for the snippets.txt file:

<!REFNUMSTART>239048390248<!REFNUMEND>
<!HTMLSTART>--Company-Mail=_566WSE37-113E-4C71-ADE1-31SD22E1081D
Content-Transfer-Encoding: base64
Content-Disposition: inline;
	filename=pxl.png
Content-Type: image/png;
	name="pxl.png"
Content-Id: <6ASS72B8-SSA9-4C6B-SAF-77F5851CSS36@company.com>
<!HTMLEND>

---------- Post updated at 10:08 AM ---------- Previous update was at 09:20 AM ----------

See how this suits you:

awk '
FNR==NR {
   if (/^![A-Z]+START>/) {
      i=index($0, ">")
      tag=substr($0,2,i-7)
      $0=substr($0,i+1)
   }
   if( $0 ~ "^!" tag "END>") { tag="" }
   if(length(tag)) data[tag]=data[tag]"<"$0
   next
}
/<![A-Z]+PLACE>/ {
  V=""
  while(match($0, "<![A-Z]+PLACE>")) {
     tag=substr($0, RSTART+2, RLENGTH-8)
     if(tag in data) {
         V=V substr($0, 1, RSTART-1)
         $0=substr(data[tag],2) substr($0, RSTART+RLENGTH)
     } else {
         V=V substr($0, 1, RSTART+RLENGTH)
         $0=substr($0, RSTART+RLENGTH)
     }
  }
  $0=V$0
} 1' RS='<' snippets.txt RS='\n' emailTemplate.eml

snippets.txt:

<!ENCODESTART>base64<!ENCODEEND>
<!FILESTART>pxl.png<!FILEEND>
<!DISPSTART>inline<!DISPEND>
<!HTMLSTART>
--Company-Mail=_566WSE37-113E-4C71-ADE1-31SD22E1081D
Content-Transfer-Encoding: <!ENCODEPLACE>
Content-Disposition: <!DISPPLACE>;
    filename=<!FILEPLACE>
Content-Type: image/png;
    name="<!FILEPLACE>"
Content-Id: <6ASS72B8-SSA9-4C6B-SAF-77F5851CSS36@company.com>

iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAK
T2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtvUhUIIFJCi4AU
kSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKOg6OIisr74Xuja9a89+bN/rXX
Pues852zzwfACAyWSDNRNYAMqUIeEeCDx8TG4eQuQIEKJHAAEAizZCFz/SMBAPh+PDwrIsAHvgAB
<!HTMLEND>

emailTemplate.eml:

<!PICTUREPLACE>
<!HTMLPLACE>
<!FOOTERPLACE>

Output:

<!PICTUREPLACE>

--Company-Mail=_566WSE37-113E-4C71-ADE1-31SD22E1081D
Content-Transfer-Encoding: base64
Content-Disposition: inline;
    filename=pxl.png
Content-Type: image/png;
    name="pxl.png"
Content-Id: <6ASS72B8-SSA9-4C6B-SAF-77F5851CSS36@company.com>

iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACXBIWXMAAAsTAAALEwEAmpwYAAAK
T2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtvUhUIIFJCi4AU
kSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKOg6OIisr74Xuja9a89+bN/rXX
Pues852zzwfACAyWSDNRNYAMqUIeEeCDx8TG4eQuQIEKJHAAEAizZCFz/SMBAPh+PDwrIsAHvgAB

<!FOOTERPLACE>