Reputation: 75
WIKI_CODE=$(cat <<ZZZWCZZZ
<!--BEGIN-->
<div class="mw-collapsible" style="text-align:left;" data-collapsetext="Hide" data-expandtext="Show">
<div class="mw-collapsible-content">
<div class="mw-collapsible mw-collapsed" style="text-align:left">
<div class="mw-collapsible-toggle" style="text-align:left; float: none;">
</div>
<span title="
BLAH TITLE FOR BLAH
1) blah...
2) blah,blah...;
blah blah blah...
3) more blah:
blah on another line...
4) EMPTY Question4
5) EMPTY Question5
">Begin Blah Questions (Hover over me)</span> [[#Hover|Hover]]
<!--END-->
ZZZWCZZZ
);
#echo "$WIKI_CODE";
WIKI_TITLE="=My Wiki Title=";
NEW_SECTION=$(echo -e "$WIKI_CODE";);
#echo "$NEW_SECTION";
sed -i "s/$WIKI_TITLE/$NEW_SECTION/g" $DOC_FILE;
I get the following error:
sed: -e expression #1, char 75: unterminated `s' command
I tried quoting everything, but it still does not work.
NEW_SECTION=$(echo $NEW_SECTION | sed -e 's/./\\&/g; 1!s/^/"/; $!s/$/"/' );
#echo "$NEW_SECTION";
sed -i "s/$WIKI_TITLE/$NEW_SECTION/g" $DOC_FILE;
I get the following error:
sed: -e expression #1, char 1607: invalid reference \9 on `s' command's RHS
Upvotes: 0
Views: 150
Reputation: 203655
You cannot do what you want reliably with sed because sed works on REs, not strings, and sed requires the search and replacement text to not contain whatever specific character you use as it's delimiter. So, just use awk instead (untested but should be close):
awk -v old="$WIKI_TITLE" -v new="$NEW_SECTION" '
s=index($0,old){$0 = substr($0,1,s-1) new substr($0,s+length(old))}
1' file
The above will work with any characters in either the search or replacement string.
With newer gawks you can use -i inplace
for pseudo-inplace editing like sed does with it's -i
arg, otherwise just add > tmp && mv tmp file
to the end of the command to change the original file.
Upvotes: 1
Reputation: 75575
The error message you are seeing is sed
trying to tell you that it ran into an ending delimiter for the script before the s
command was terminated.
Since your beginning delimiter was "
, that implies that it is choking on one of the "
in your replacement text. That implies that you need to use \
to escape all the "
characters in your replacement text.
I created a file called DocFile
and put the following content inside it:
=My Wiki Title=
I then ran the following commands, which successfully injected the contents of $NEW_SECTION
into the file. Note that I use the delimiter @
in the final sed
command, as shellter suggested, because the @
character does not appear in your text. Observe also that there will be literal characters \"
inside $NEW_SECTION
after the second and third statements, but the final statement effectively consumes them, so they do not appear in the final output.
NEW_SECTION=$(echo -e "$WIKI_CODE";);
NEW_SECTION=$(echo $NEW_SECTION | sed -e 's!"!\\"!g' );
NEW_SECTION=$(echo $NEW_SECTION | sed -e 's/&/\\&/g')
sed -i "s@$WIKI_TITLE@$NEW_SECTION@g" DocFile
Upvotes: 1