troubleshooting Question

Sed to replace text as it is without newline conversion

Avatar of enthuguy
enthuguyFlag for Australia asked on
* BashShell ScriptingJSON* sed* python2
10 Comments1 Solution81 ViewsLast Modified:
Hi,

Due to some product limitation
1. have to apply newline char in an xml content and store in a file updated.xml. So the entire content will be in a singleline.
2. Then I use sed command to update this content in a JSON file.
3. this step is to invoke an api to send the above json (right now it fails)

But what happens, after sed in the json file, I think it converts new line and I see each xml in a separate line which make this invalid request.

could you advice how to store xml content as is like in a single line in json please

Sed command
dtoXMLData=$(cat escaped/${response_xml_for_conversion})
sed -i -e "s|CHANGE_ME_DTO_XML_DATA|${dtoXMLData}|g" ${src_finalize_project_json}

Converted XML -
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n<packageItemStatuses>\r\n<importProcessId>461a831c-04d8-455f-9ea9-09afe75b2b90</importProcessId>\r\n<packageItemStatus>\r\n<conflicts>\r\n<dest>0</dest>\r\n<id>9003</id>\r\n<source>20191115000000</source>\r\n</conflicts>\r\n<conflicts>\r\n<dest>0</dest>\r\n<id>9004</id>\r\n<source>20191115000000</source>\r\n</conflicts>\r\n<conflicts>\r\n<dest>0</dest>\r\n<id>9005</id>\r\n<source>20191115000000</source>\r\n</conflicts>\r\n<conflicts>\r\n<dest>0</dest>\r\n<id>9006</id>\r\n<source>1000</source>\r\n</conflicts>\r\n<conflicts>\r\n<id>9007</id>\r\n</conflicts>\r\n<exists>true</exists>\r\n<id>157752420</id>\r\n<merge>true</merge>\r\n<skip>false</skip>\r\n<type>21</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>true</exists>\r\n<id>158010210</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>19</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>true</exists>\r\n<id>159064232</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>20</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>false</exists>\r\n<id>158009962</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>19</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>false</exists>\r\n<id>159062289</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>20</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>true</exists>\r\n<id>158009960</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>19</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>true</exists>\r\n<id>159066053</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>20</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>true</exists>\r\n<id>158009958</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>19</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>true</exists>\r\n<id>159062265</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>20</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>true</exists>\r\n<id>159062351</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>20</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>true</exists>\r\n<id>157995292</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>19</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>true</exists>\r\n<id>159062282</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>20</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>true</exists>\r\n<id>157995289</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>19</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>true</exists>\r\n<id>159062298</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>20</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>true</exists>\r\n<id>158009964</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>19</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>true</exists>\r\n<id>159077274</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>20</type>\r\n</packageItemStatus>\r\n<packageItemStatus>\r\n<exists>true</exists>\r\n<id>159066050</id>\r\n<merge>false</merge>\r\n<skip>false</skip>\r\n<type>20</type>\r\n</packageItemStatus>\r\n</packageItemStatuses>

after sed with incorrect mulitline
"body": {
                                        "mode": "raw",
                                        "raw": '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>^M
<packageItemStatuses>^M
<importProcessId>461a831c-04d8-455f-9ea9-09afe75b2b90</importProcessId>^M
<packageItemStatus>^M
<conflicts>^M
<dest>0</dest>^M
<id>9003</id>^M
<source>20191115000000</source>^M
</conflicts>^M
<conflicts>^M
<dest>0</dest>^M
<id>9004</id>^M
<source>20191115000000</source>^M
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros