-1
#/!bin/sh
# infile is "person's clothes"
infile=$1
outfile=$2
sed -z "s|'|\\\\'|g;" <$infile >temp.txt
txt=`cat temp.txt`
echo $txt
# displays person\'s clothes
sed -e "s|paste_area|$txt|" blank.html >"$outfile"
# the relevant part of $outfile is "person's clothes" expecting "person\'s clothes"

"please add some context to explain the code sections" please read the code it has comments included

  • 1
    You need to double the backslash in `$txt`. Otherwise it's an escape character, and `\'` means just `'` – Barmar Feb 09 '22 at 16:19
  • Not your immediate problem, but `echo $txt` should be `echo "$txt"` -- see [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Feb 09 '22 at 16:25

1 Answers1

2

You need to double all the backslashes in $txt to make them literal when used in the sed replacement string. Otherwise they'll be treated as the escape prefix.

sed -e "s|paste_area|${txt/\\/\\\\}|" blank.html >"$outfile"
Barmar
  • 741,623
  • 53
  • 500
  • 612