How to pass base64 encoded content to sed?

The XFILEBASE64 variable has base64 encoded content, and I want to replace some string with base64 content.

Of course, base64 is packed with special characters, and I tried to use $ '\ 001' as a 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: it seems the problem has nothing to do with sed, it should 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 
+7
bash shell base64 sed
source share
4 answers

The problem was base64 encoded, -w 0 the base64 option did the trick.

 cat ./sample.xml | base64 -w 0 
+2
source share

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

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

Team

 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 

To say, $'...' in double quotes are not special.

+1
source share

All Articles