I would like to append multiple lines after a matched line in a text file in a bash script. While I don't particularly care what tool to choose for the job, the important thing to me is that I want to specify the lines appended "as is" inside the script (so without generating an additional file what would hold them) so they end up in a Bash variable, and without having to quote/escape anything in them - and for that purpose using a 'quoted' heredoc would work for me. Here is an example, appendtest.sh:
cat > mytestfile.txt <<'EOF'
"'iceberg'"
"'ice cliff'"
"'ice field'"
"'inlet'"
"'island'"
"'islet'"
"'isthmus'"
EOF
IFS='' read -r -d '' REPLACER <<'EOF'
"'$oasis$'"
"'$ocean$'"
"'$oceanic trench$'"
EOF
echo "$REPLACER"
sed -i "/ \"'ice field'\"/a${REPLACER}" mytestfile.txt
This unfortunately does not work:
$ bash appendtest.sh
"'$oasis$'"
"'$ocean$'"
"'$oceanic trench$'"
sed: -e expression #1, char 39: unknown command: `"'
... because sed failed when an unescaped multiline replacement is used. So my question is:
- What could I use instead of
sedto perform matching on a line of text, and inserting/appending the lines as specified in the Bash variable ($REPLACERin the example)?