Insert a newline in XML

Hi guys,

I got a requirement that

]]></InputMessage><ErrorMessage><ErrorHeader><ExceptionNumber>2951</ExceptionNumber><TimeStamp>2013-07-03T21:49:10.814803</TimeStamp><BrokerName>WMQPCOM03_BK</BrokerName><BrokerFamily>UNIX</BrokerFamily><ExecutionGroup>WMQPCOM03_TCOM_FULFILLMENT</ExecutionGroup><QueueManager>WMQPCOM03</QueueManager><InputQueue>TCOM.FULFILLMENT.OMS.WMS.SA.SUB</InputQueue><MessageFlow>com.tgt.tcom.il.shipment.TCOM_SA_OMS_WMS_Subscribe_MsgFlow</MessageFlow><MessageId>414d5120574d5150434f4d30332020209371c7515f86dc34</MessageId><ErrorSeverity>3</ErrorSeverity><ErrorCategory>UserException</ErrorCategory><ErrorSource>com.tgt.tcom.il.shipment.TCOM_SA_OMS_WMS_Subscribe_MsgFlow.TCOM_SA_Transform_Batch_SubFlow.Transform_SA_ToFC</ErrorSource></ErrorHeader><ErrorDetails><RecoverableException><File>/build/S700_P/src/DataFlowEngine/ImbComputeNode.cpp</File><Line>489</Line><Function>ImbComputeNode::evaluate</Function><Type>ComIbmComputeNode</Type><Name>com/tgt/tcom/il/shipment/TCOM_SA_OMS_WMS_Subscribe_MsgFlow#FCMComposite_1_1.com/tgt/tcom/il/shipment/TCOM_SA_Transform_Batch_SubFlow#FCMComposite_1_2</Name><Label>com.tgt.tcom.il.shipment.TCOM_SA_OMS_WMS_Subscribe_MsgFlow.TCOM_SA_Transform_Batch_SubFlow.Transform_SA_ToFC</Label><Catalog>BIPmsgs</Catalog><Severity>3</Severity><Number>2230</Number><Text>Caught exception and rethrowing</Text><RecoverableException><File>/build/S700_P/src/DataFlowEngine/ImbRdl/ImbRdlStatementGroup.cpp</File><Line>641</Line><Function>SqlStatementGroup::execute</Function><Type>ComIbmComputeNode</Type><Name>com/tgt/tcom/il/shipment/TCOM_SA_OMS_WMS_Subscribe_MsgFlow#FCMComposite_1_1.com/tgt/tcom/il/shipment/TCOM_SA_Transform_Batch_SubFlow#FCMComposite_1_2</Name><Label>com.tgt.tcom.il.shipment.TCOM_SA_OMS_WMS_Subscribe_MsgFlow.TCOM_SA_Transform_Batch_SubFlow.Transform_SA_ToFC</Label><Catalog>BIPmsgs</Catalog><Severity>3</Severity><Number>2488</Number><Text>Error detected, rethrowing</Text><Insert><Type>5</Type><Text>com.tgt.tcom.il.shipment.Transform_SA_ToFC.Main</Text></Insert><Insert><Type>5</Type><Text>175.4</Text></Insert><Insert><Type>5</Type><Text>THROW EXCEPTION  MESSAGE 2951 VALUES( 'Error Message', 'SterlingMessage.OrderRelease.PersonInfoShipTo.FirstNameANDLasteName/Addr1/City/State/Country/ZipCode value is empty or state value>2');</Text></Insert><UserException><File>/build/S700_P/src/DataFlowEngine/ImbRdl/ImbRdlThrowExceptionStatements.cpp</File><Line>229</Line><Function>SqlThrowExceptionStatement::execute</Function><Type>ComIbmComputeNode</Type><Name>com/tgt/tcom/il/shipment/TCOM_SA_OMS_WMS_Subscribe_MsgFlow#FCMComposite_1_1.com/tgt/tcom/il/shipment/TCOM_SA_Transform_Batch_SubFlow#FCMComposite_1_2</Name><Label>com.tgt.tcom.il.shipment.TCOM_SA_OMS_WMS_Subscribe_MsgFlow.TCOM_SA_Transform_Batch_SubFlow.Transform_SA_ToFC</Label><Catalog>BIPmsgs</Catalog><Severity>1</Severity><Number>2951</Number><Text>User generated exception</Text><Insert><Type>5</Type><Text>Error Message</Text></Insert><Insert><Type>5</Type><Text>SterlingMessage.OrderRelease.PersonInfoShipTo.FirstNameANDLasteName/Addr1/City/State/Country/ZipCode value is empty or state value>2</Text></Insert></UserException></RecoverableException></RecoverableException></ErrorDetails></ErrorMessage></Message>
<Message><InputMessage><![CDATA[<?xml version="1.0"encoding="UTF8"?>

In the above xml , i need to insert newline("\n") after </Message> tag is found.
I tried with sed command but its inserting after the entireline.

What i need is like below

]]></InputMessage><ErrorMessage><ErrorHeader><ExceptionNumber>2951</ExceptionNumber><TimeStamp>2013-07-03T21:49:10.814803</TimeStamp><BrokerName>WMQPCOM03_BK</BrokerName><BrokerFamily>UNIX</BrokerFamily><ExecutionGroup>WMQPCOM03_TCOM_FULFILLMENT</ExecutionGroup><QueueManager>WMQPCOM03</QueueManager><InputQueue>TCOM.FULFILLMENT.OMS.WMS.SA.SUB</InputQueue><MessageFlow>com.tgt.tcom.il.shipment.TCOM_SA_OMS_WMS_Subscribe_MsgFlow</MessageFlow><MessageId>414d5120574d5150434f4d30332020209371c7515f86dc34</MessageId><ErrorSeverity>3</ErrorSeverity><ErrorCategory>UserException</ErrorCategory><ErrorSource>com.tgt.tcom.il.shipment.TCOM_SA_OMS_WMS_Subscribe_MsgFlow.TCOM_SA_Transform_Batch_SubFlow.Transform_SA_ToFC</ErrorSource></ErrorHeader><ErrorDetails><RecoverableException><File>/build/S700_P/src/DataFlowEngine/ImbComputeNode.cpp</File><Line>489</Line><Function>ImbComputeNode::evaluate</Function><Type>ComIbmComputeNode</Type><Name>com/tgt/tcom/il/shipment/TCOM_SA_OMS_WMS_Subscribe_MsgFlow#FCMComposite_1_1.com/tgt/tcom/il/shipment/TCOM_SA_Transform_Batch_SubFlow#FCMComposite_1_2</Name><Label>com.tgt.tcom.il.shipment.TCOM_SA_OMS_WMS_Subscribe_MsgFlow.TCOM_SA_Transform_Batch_SubFlow.Transform_SA_ToFC</Label><Catalog>BIPmsgs</Catalog><Severity>3</Severity><Number>2230</Number><Text>Caught exception and rethrowing</Text><RecoverableException><File>/build/S700_P/src/DataFlowEngine/ImbRdl/ImbRdlStatementGroup.cpp</File><Line>641</Line><Function>SqlStatementGroup::execute</Function><Type>ComIbmComputeNode</Type><Name>com/tgt/tcom/il/shipment/TCOM_SA_OMS_WMS_Subscribe_MsgFlow#FCMComposite_1_1.com/tgt/tcom/il/shipment/TCOM_SA_Transform_Batch_SubFlow#FCMComposite_1_2</Name><Label>com.tgt.tcom.il.shipment.TCOM_SA_OMS_WMS_Subscribe_MsgFlow.TCOM_SA_Transform_Batch_SubFlow.Transform_SA_ToFC</Label><Catalog>BIPmsgs</Catalog><Severity>3</Severity><Number>2488</Number><Text>Error detected, rethrowing</Text><Insert><Type>5</Type><Text>com.tgt.tcom.il.shipment.Transform_SA_ToFC.Main</Text></Insert><Insert><Type>5</Type><Text>175.4</Text></Insert><Insert><Type>5</Type><Text>THROW EXCEPTION  MESSAGE 2951 VALUES( 'Error Message', 'SterlingMessage.OrderRelease.PersonInfoShipTo.FirstNameANDLasteName/Addr1/City/State/Country/ZipCode value is empty or state value>2');</Text></Insert><UserException><File>/build/S700_P/src/DataFlowEngine/ImbRdl/ImbRdlThrowExceptionStatements.cpp</File><Line>229</Line><Function>SqlThrowExceptionStatement::execute</Function><Type>ComIbmComputeNode</Type><Name>com/tgt/tcom/il/shipment/TCOM_SA_OMS_WMS_Subscribe_MsgFlow#FCMComposite_1_1.com/tgt/tcom/il/shipment/TCOM_SA_Transform_Batch_SubFlow#FCMComposite_1_2</Name><Label>com.tgt.tcom.il.shipment.TCOM_SA_OMS_WMS_Subscribe_MsgFlow.TCOM_SA_Transform_Batch_SubFlow.Transform_SA_ToFC</Label><Catalog>BIPmsgs</Catalog><Severity>1</Severity><Number>2951</Number><Text>User generated exception</Text><Insert><Type>5</Type><Text>Error Message</Text></Insert><Insert><Type>5</Type><Text>SterlingMessage.OrderRelease.PersonInfoShipTo.FirstNameANDLasteName/Addr1/City/State/Country/ZipCode value is empty or state value>2</Text></Insert></UserException></RecoverableException></RecoverableException></ErrorDetails></ErrorMessage></Message>
 
<Message><InputMessage><![CDATA[<?xml version="1.0" encoding="UTF-8"?>

Thank u guys.

$ sed "s/^<Message>/\\
&/" file
]]></InputMessage><ErrorMessage><ErrorHeader><ExceptionNumber>2951</ExceptionNumber><TimeStamp>2013-07-03T21:49:10.814803</TimeStamp><BrokerName>WMQPCOM03_BK</BrokerName><BrokerFamily>UNIX</BrokerFamily><ExecutionGroup>WMQPCOM03_TCOM_FULFILLMENT</ExecutionGroup><QueueManager>WMQPCOM03</QueueManager><InputQueue>TCOM.FULFILLMENT.OMS.WMS.SA.SUB</InputQueue><MessageFlow>com.tgt.tcom.il.shipment.TCOM_SA_OMS_WMS_Subscribe_MsgFlow</MessageFlow><MessageId>414d5120574d5150434f4d30332020209371c7515f86dc34</MessageId><ErrorSeverity>3</ErrorSeverity><ErrorCategory>UserException</ErrorCategory><ErrorSource>com.tgt.tcom.il.shipment.TCOM_SA_OMS_WMS_Subscribe_MsgFlow.TCOM_SA_Transform_Batch_SubFlow.Transform_SA_ToFC</ErrorSource></ErrorHeader><ErrorDetails><RecoverableException><File>/build/S700_P/src/DataFlowEngine/ImbComputeNode.cpp</File><Line>489</Line><Function>ImbComputeNode::evaluate</Function><Type>ComIbmComputeNode</Type><Name>com/tgt/tcom/il/shipment/TCOM_SA_OMS_WMS_Subscribe_MsgFlow#FCMComposite_1_1.com/tgt/tcom/il/shipment/TCOM_SA_Transform_Batch_SubFlow#FCMComposite_1_2</Name><Label>com.tgt.tcom.il.shipment.TCOM_SA_OMS_WMS_Subscribe_MsgFlow.TCOM_SA_Transform_Batch_SubFlow.Transform_SA_ToFC</Label><Catalog>BIPmsgs</Catalog><Severity>3</Severity><Number>2230</Number><Text>Caught exception and rethrowing</Text><RecoverableException><File>/build/S700_P/src/DataFlowEngine/ImbRdl/ImbRdlStatementGroup.cpp</File><Line>641</Line><Function>SqlStatementGroup::execute</Function><Type>ComIbmComputeNode</Type><Name>com/tgt/tcom/il/shipment/TCOM_SA_OMS_WMS_Subscribe_MsgFlow#FCMComposite_1_1.com/tgt/tcom/il/shipment/TCOM_SA_Transform_Batch_SubFlow#FCMComposite_1_2</Name><Label>com.tgt.tcom.il.shipment.TCOM_SA_OMS_WMS_Subscribe_MsgFlow.TCOM_SA_Transform_Batch_SubFlow.Transform_SA_ToFC</Label><Catalog>BIPmsgs</Catalog><Severity>3</Severity><Number>2488</Number><Text>Error detected, rethrowing</Text><Insert><Type>5</Type><Text>com.tgt.tcom.il.shipment.Transform_SA_ToFC.Main</Text></Insert><Insert><Type>5</Type><Text>175.4</Text></Insert><Insert><Type>5</Type><Text>THROW EXCEPTION  MESSAGE 2951 VALUES( 'Error Message', 'SterlingMessage.OrderRelease.PersonInfoShipTo.FirstNameANDLasteName/Addr1/City/State/Country/ZipCode value is empty or state value>2');</Text></Insert><UserException><File>/build/S700_P/src/DataFlowEngine/ImbRdl/ImbRdlThrowExceptionStatements.cpp</File><Line>229</Line><Function>SqlThrowExceptionStatement::execute</Function><Type>ComIbmComputeNode</Type><Name>com/tgt/tcom/il/shipment/TCOM_SA_OMS_WMS_Subscribe_MsgFlow#FCMComposite_1_1.com/tgt/tcom/il/shipment/TCOM_SA_Transform_Batch_SubFlow#FCMComposite_1_2</Name><Label>com.tgt.tcom.il.shipment.TCOM_SA_OMS_WMS_Subscribe_MsgFlow.TCOM_SA_Transform_Batch_SubFlow.Transform_SA_ToFC</Label><Catalog>BIPmsgs</Catalog><Severity>1</Severity><Number>2951</Number><Text>User generated exception</Text><Insert><Type>5</Type><Text>Error Message</Text></Insert><Insert><Type>5</Type><Text>SterlingMessage.OrderRelease.PersonInfoShipTo.FirstNameANDLasteName/Addr1/City/State/Country/ZipCode value is empty or state value>2</Text></Insert></UserException></RecoverableException></RecoverableException></ErrorDetails></ErrorMessage></Message>

<Message><InputMessage><![CDATA[<?xml version="1.0"encoding="UTF8"?>

Hi thanks for ur help
I tried

sed "s/^<\/Message>/\\ &/" TCOM.FULFILLMENT.OMS.SA.ERROR.xml >t.xml

But i didnt get the result.Could you please find out where it went wrong.

&/" TCOM.FULFILLMENT.OMS.SA.ERROR.xml >t.xml

should be on the next line in your script or on commandline

I tried in next line.
It inserts before the <Message> tag.
I need to insert after </Message> tag the code which u gave inserting before <Message> tag.
One more thing about my requirement is </Message> tag is found many times in my code.

---------- Post updated at 12:10 PM ---------- Previous update was at 06:45 AM ----------

hi guys i got the result using the below code,

 
insert="\n<Message>"
nawk -v f="$insert" '{/<\/\Message>/x}{sub("<Message>",f)}1' file.xml >file2.xml

Thanks for ur timely help guys.

Since the shell does not need to expand anything with them, there's no reason to use double quotes.

Habitually using double quotes when shell expansion isn't intended will eventually lead to a problematic unintentional expansion (usually at some future time when you're stressed and making a quick edit).

Single quotes are also a bit more efficient, because the shell only needs to check if the current character is a single quote. Whereas when parsing a double quoted string, besides having to check if the current character is a double quote, the shell must watch for the beginning of an $-expansion (parameter substitution, command substitution, and arithmetic substitution), backtick command substitution, and backslashes which depending on what follows them may or may not be special. In some shells, you can add history expansion to the list.

Even in the case of your simple code, it has led to the unnecessary doubling of a backslash.

sed 's/^<Message>/\
&/' file

Regards,
Alister

1 Like