Insert a newline in XML


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert a newline in XML
# 1  
Old 07-05-2013
Insert a newline in XML

Hi guys,

I got a requirement that
Code:
]]></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( &apos;Error Message&apos;, &apos;SterlingMessage.OrderRelease.PersonInfoShipTo.FirstNameANDLasteName/Addr1/City/State/Country/ZipCode value is empty or state value&gt;2&apos;);</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&gt;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
Code:
]]></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( &apos;Error Message&apos;, &apos;SterlingMessage.OrderRelease.PersonInfoShipTo.FirstNameANDLasteName/Addr1/City/State/Country/ZipCode value is empty or state value&gt;2&apos;);</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&gt;2</Text></Insert></UserException></RecoverableException></RecoverableException></ErrorDetails></ErrorMessage></Message>
 
<Message><InputMessage><![CDATA[<?xml version="1.0" encoding="UTF-8"?>

Thank u guys.
# 2  
Old 07-05-2013
Code:
$ 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( &apos;Error Message&apos;, &apos;SterlingMessage.OrderRelease.PersonInfoShipTo.FirstNameANDLasteName/Addr1/City/State/Country/ZipCode value is empty or state value&gt;2&apos;);</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&gt;2</Text></Insert></UserException></RecoverableException></RecoverableException></ErrorDetails></ErrorMessage></Message>

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

# 3  
Old 07-05-2013
Hi thanks for ur help
I tried
Code:
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.
# 4  
Old 07-05-2013
Code:
&/" TCOM.FULFILLMENT.OMS.SA.ERROR.xml >t.xml

should be on the next line in your script or on commandline
# 5  
Old 07-05-2013
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,
Code:
 
insert="\n<Message>"
nawk -v f="$insert" '{/<\/\Message>/x}{sub("<Message>",f)}1' file.xml >file2.xml

Thanks for ur timely help guys.
# 6  
Old 07-05-2013
Quote:
Originally Posted by anbu23
Code:
$ sed "s/^<Message>/\\
&/" file

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.
Code:
sed 's/^<Message>/\
&/' file

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a newline after match in files of specific name under some subdirectories?

Hi I'd like to add the newline: \tuser: nobody", or "<TAB>user: nobody to all files named: docker-compose.ymlin subfolders of pwd with names beginning with 10-20. Within these files, I'd like to find the line (there'll only be one) containing: command: celery workerNOTE: As far as... (2 Replies)
Discussion started by: duncanbetts
2 Replies

2. Shell Programming and Scripting

Insert newline when grep result is empty

Given a csv file with 40 columns with name, address, hometown etc. I use a bash command in 1 line which: 1. gets the address column and pipes that to 2. grep the first digit and everything that follows Command: awk -F ";" '{print $19}' /Users/jb/Desktop/ReorderTempTotal.csv | grep -o "\d.*"... (7 Replies)
Discussion started by: JBVeenstra
7 Replies

3. Shell Programming and Scripting

How to insert new line in xml?

I have an xml like below and wanted to insert a new line between end of the tag and start of the new tag. <partyaddress><addressline1>gandhi stree</addressline1><city>hyderbad</city><state>AP</state><addressline1>xyz stree</addressline1><city>bangalore</city><state>Karnataka</state>... (6 Replies)
Discussion started by: D_Sethi
6 Replies

4. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

5. Shell Programming and Scripting

Script using Sed :Search all patterns & after the last Patter, insert a newLine with Comma Sep Value

I am trying to search the pattern "ARS (11)" and after the LAST pattern, i am trying to open new line and enter text using sed. My Existing Text file is Users.txtpaul, Paul Smith, Stevn Smiley, REQ000001, ARS (11) sam, Sam Martin, Stevn Smiley, REQ000001, ARS (11) mike, Mike Conway, Stevn... (8 Replies)
Discussion started by: evrurs
8 Replies

6. Shell Programming and Scripting

sed insert text without newline

Hi, I use sed to insert text at beginning of a file. But sed inserts a newline after my text that I do not need. For example, I want to insert "foo" at the beginning of my file: > cat myfile This is first line. > sed -i '1i\foo' myfile > cat myfile foo This is first line. ... (5 Replies)
Discussion started by: tdw
5 Replies

7. Shell Programming and Scripting

AWK Script Issue insert newline for a regular expression match

Hi , I am having an issue with the Awk script to insert newline for a regular expression match Having a file like this FILE1 #################### RXOER , RXERA , RXERC , RXERD .RXEA(RXBSN), RXERD , REXCD input RXEGT buffer RXETRY ####################### Want to match the RXE... (38 Replies)
Discussion started by: jaita
38 Replies

8. Shell Programming and Scripting

How to enter a newline after every XML tag end?

Hi Guyz, I have an XML message in following format: I want my contents to be formatted in following order: i.e. I want a newline after every XML tag end. How to do this? Thnx in advance. (5 Replies)
Discussion started by: DTechBuddy
5 Replies

9. Shell Programming and Scripting

Insert two newline after some pattern

Hi, I need to insert two newline characters after matching of a pattern in each line of a file. Eg. If i have a file with contents as follows:- Now, i want output as follows :- i.e., I need to insert two newline characters after the occurance of pattern "</Message>>". Thnx... (1 Reply)
Discussion started by: DTechBuddy
1 Replies

10. Shell Programming and Scripting

use regexp to insert newline within a line

I have successfully used regexp and sed to insert a newline before or after a line containing a matched pattern /WORD/. However, I want to insert a newline immediately following /WORD/ and not after the -line- containing the pattern matched. I can match a pattern, but it is matched via a wild card... (2 Replies)
Discussion started by: kpeirce
2 Replies
Login or Register to Ask a Question