Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 573
user52028778 Avatar asked Aug 27 '15 14:08

user52028778


1 Answers

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

cat ./sample.xml | base64 -w 0
like image 108
user52028778 Avatar answered Nov 07 '22 01:11

user52028778