linux - Sed: How to replace pattern with file's contemt -


file 'sample.json' (json):

{     "jsonrpc": "2.0",     "method": "configuration.import",     "params": {        "format": "xml",        "source": "replace_me_with_xmlsource"     },     "auth": "91ea4764dcab42e8317b399c42985792",     "id": 1 } 

file 'source.xml' (xml, 1 long line spaces , double quotes):

<?xml version="1.0" encoding="utf-8"?><zabbix_export>...</zabbix_export> 

required result (json xml param's value):

    ...    "source": "<?xml version=\"1.0\" encoding=\"utf-8\"?><zabbix_export>...</zabbix_export>" ... 

i tried place content of 'source.xml' variable , use in sed, without success:

# x=$(cat source.xml) # sed "s/replace_me_with_xmlsource/$x/" sample.json sed: -e expression #1, char 80: unknown option `s' # sed "s/xmlsource/"$x"/" sample.json sed: -e expression #1, char 17: unterminated `s' command #  sed "s/xmlsource/"$x"/" sample.json ... 

i tried use sed '/replace_me_with_xmlsource/ r source.xml' in variations, sed-fu not enough solve it..

i tried google , search here, but.. see question..

thanks advice

# read xml file variable xml=$(< source.xml) # perform substitution, adding appropriate escaping of quotes sed 's|\("source": \).*|\1"'"${xml//\"/\\\\\"}"'"|' sample.json  

Comments