Giorgi Gviani
Giorgi Gviani

Reputation: 28404

How to pass base64 encoded content to sed?

Variable XFILEBASE64 has base64 encoded content and I want to replace some string with that base64 content.

Sure enough base64 is packed with special characters, and I've tried $'\001' as delimiter, still getting the error message. Any suggestions?

XFILEBASE64=`cat ./sample.xml | base64`
cat ./template.xml  | sed "s$'\001'<Doc>###%DOCDATA%###<\/Doc>$'\001'<Doc>${XFILEBASE64}<\/Doc>$'\001'g"
> sed: -e expression #1, char 256: unterminated `s' command

EDIT: Looks like problem has nothing to do with sed, it must be hidden in base64 operations.

sample.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a>testsdfasdfasdfasfasdfdasfdads</a>     

To reproduce the problem:

foo=`base64 ./sample.xml`
echo $foo | base64 --decode

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<base64: invalid input

Upvotes: 8

Views: 8970

Answers (5)

NStein
NStein

Reputation: 11

Adding to user52028778's comment as I had to do more reading to diagnose what the issue was. The problem is in the base64 command. The default base64 command includes line wraps after 76 characters for some linux distros. This is inserting a line termination into the sed command later when the variable is used causing it to fail.

cat ./sample.xml | base64 --wrap=0

https://linux.die.net/man/1/base64

Upvotes: 1

Giorgi Gviani
Giorgi Gviani

Reputation: 28404

The problem was in base64 encoding, -w 0 option of base64 did the trick.

cat ./sample.xml | base64 -w 0

Upvotes: 7

pynexj
pynexj

Reputation: 20768

The command

sed "s$'\001'<Doc>###%DOCDATA%###<\/Doc>$'\001'<Doc>${XFILEBASE64}<\/Doc>$'\001'g"

should be written as

sed s$'\001'"<Doc>###%DOCDATA%###<\/Doc>"$'\001'"<Doc>${XFILEBASE64}<\/Doc>"$'\001'g

That's to say, $'...' in double quotes are not special.

Upvotes: 1

NeronLeVelu
NeronLeVelu

Reputation: 10039

Tempo64="$( echo "${XFILEBASE64}" | sed 's/[\\&*./+!]/\\&/g' )"
sed "s!<Doc>###%DOCDATA%###</Doc>!<Doc>${Tempo64}</Doc>!g" ./template.xml
  • should work and posix compliant

Upvotes: 3

pacholik
pacholik

Reputation: 8982

Base64 has only three special characters (wikipedia) - usually +, / and =. You can use for example @, , & or ; with no problem.

Upvotes: 3

Related Questions