Find replace text in xml file on the fly


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Find replace text in xml file on the fly
# 1  
Old 02-12-2019
Find replace text in xml file on the fly

Dear Unix guru,

I have a .XML file which is being used to load data to oracle. This file comes on unix box and one of the tag in xml is oracle key word. I want to find that tag and replace with new tag on the fly
For example
I will get one of the tag in xml is as below
Code:
<from>Test Test 123</from>

since "from" is oracle key word i am having difficulties to read this so i want to replace this as below
Code:
<from_msg> Test Test 123</from_msg>

I also want to check various point like
<from > , < from > , < from>
i guess there can be space between tag and text.

I am not at all unix guy so can write any shell scripts, Any small scripts would really help me to keep moving

Thanks for the understanding




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!
You have SIX infractions by now for not using them; it's just because you were absent for >2 years that this note has become a small informal reminder only!

Last edited by RudiC; 02-12-2019 at 12:42 PM.. Reason: Added CODE tags.
# 2  
Old 02-12-2019
Is that space in front of "Test" in the desired output on purpose or is it a typo? You didn't say you want one in the written spec.



Code:
sed -r 's/< *from *>([^<]*)<\/ *from *>/<from_msg>\1<\/from_msg>/g' file
<from_msg>Test Test 123</from_msg>

# 3  
Old 02-12-2019
Hi

I am checking for space just because typo ,so that job would not fail

Any suggestions
# 4  
Old 02-21-2019
Hi All,

Can any body help me with the below error
Code:
 sed -r 's/< *from *>([^<]*)<\/ *from *>/<from_msg>\1<\/from_msg>/g' message.xml>output.xml

output

Code:
sed: illegal option -- r




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-21-2019 at 10:15 AM.. Reason: Added CODE tags.
# 5  
Old 02-21-2019
That option is needed for sed to understand EREs; pls check if -E will do instead.
# 6  
Old 02-21-2019
Hi

Not working

Code:
 sed -E's/< *from *>([^<]*)<\/ *from *>/<from_msg>\1<\/from_msg>/g' message.xml>output.xml

Code:
sed: illegal option -- E


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-21-2019 at 10:35 AM.. Reason: Added CODE tags.
# 7  
Old 02-21-2019
Is there a space missing between -E and the ' or is that a typo?
Howsoever, try leaving out any option but escape the parentheses.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to write in other language in text/xml file by reading english text/xml file using C++?

Hello Team, I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file. Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. UNIX for Beginners Questions & Answers

Find and replace a string in a text file

Dear all, I want to find all the "," in my text file and then replace the commas to a tab. I found a script online but I don't know how to modify the script for my case. Any one can help? Thank you. @echo off &setlocal set "search=%1" set "replace=%2" set "textfile=Input.txt" set... (2 Replies)
Discussion started by: forevertl
2 Replies

3. Shell Programming and Scripting

Find and replace in xml

Hi All, My XML file looks like below: <logEvent xsi:type="logservice:LogEvent" timestamp="1394713811052" severity="3" messageCode="TM_6228" message="Writing session output to log file ." user="" stacktrace="" service="" serviceType="IS" clientNode="dev" pid="712" threadName="DIRECTOR"... (3 Replies)
Discussion started by: galaxy_rocky
3 Replies

4. Linux

Linux command to find and replace occurance of more than two equal sign with "==" from XML file.

Please help me, wasted hrs:wall:, to find this soulution:- I need a command that will work on file (xml) and replace multiple occurrence (more than 2 times) Examples 1. '===' 2. '====' 3. '=======' should be replaced by just '==' Note :- single character should be replaced. (=... (13 Replies)
Discussion started by: RedRocks!!
13 Replies

5. UNIX for Advanced & Expert Users

Find and replace the line in text file

I have two files a.txt b.txt I want to find a line in a.txt and replace by another line from b.txt a.txt asfsdfsfsfdfsf asfwererfgdgf wrerwetretfdg b.txt werdfgdfgf werergfdgd sfdfgfgfgfgg i want to replace the 1st line of a.txt by 1st line of b.txt i want out put as (5 Replies)
Discussion started by: rammm
5 Replies

6. Shell Programming and Scripting

Find Node and replace line(s) preceding in xml file

Hello, I have an xml file whose contacts are like below: <Node>Apple <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Mango <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Apple <B>Value1</B> <B>Value2</B> <B>Value3</B> </Node> <Node>Bannana (3 Replies)
Discussion started by: umarsatti
3 Replies

7. Shell Programming and Scripting

find from file and replace with specific text

Dear All, I do not have any knowledge of scripting. I want to replace specific lines of a text file with a specific text. Like I have one file which is "original file" and one file "changes file" which has list of lines which I want to replace in original file with a specific string. I want the... (5 Replies)
Discussion started by: libras
5 Replies

8. Shell Programming and Scripting

Replace text inside XML file based on condition

Hi All, I want to change the name as SEQ_13 ie., <Property Name="Name">SEQ_13</Property> when the Stage Type is PxSequentialFile ie., <Property Name="StageType">PxSequentialFile</Property> :wall: Input.XML <Main> <Record Identifier="V0S13" Type="CustomStage" Readonly="0">... (3 Replies)
Discussion started by: kmsekhar
3 Replies

9. Shell Programming and Scripting

Find/Replace in XML file

I am not sure how to approach this find/replace using a shell script. Any help or guidance appreciated. I want to find this: <objects/> <thumb>thumb_0001.jpg</thumb> <preview>preview_0001.jpg</preview> And replace with something like this: <objects>... (1 Reply)
Discussion started by: jimraynor
1 Replies

10. Shell Programming and Scripting

how to replace a text inside a file based on a xml key

<c-param> <param-name>Number</param-name> <param-value>22</param-value> <description>my house number</description> </c-param> <c-param> <param-name>Address</param-name> ... (4 Replies)
Discussion started by: reldb
4 Replies
Login or Register to Ask a Question