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

<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

<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

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.

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

Hi

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

Any suggestions

Hi All,

Can any body help me with the below error

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

output

sed: illegal option -- r

That option is needed for sed to understand EREs; pls check if -E will do instead.

Hi

Not working

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

Is there a space missing between -E and the ' or is that a typo?
Howsoever, try leaving out any option but escape the parentheses.

Hi

I omitted the option and ran the below command

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

and the command is coming out with error

 sed: command garbled: s/< *from *>([^<]*)<\/ *from *>/<from_msg>\1<\/from_msg>/g
 

Did you read and fully understand my post?

Some basic thought, that comes to my mind:

Even if I know nothing about oracle, I wonder why the software should complain about any string content. It should only complain - in my view - if the data to load are somehow interpreted as (SQL-Code, so this may be an error of quoting. I do not see why a large db column with xml-encoded data should be something special which could and should not be imported.

Hi,

I tried running the command with space and without space and still getting same error
sed: illegal option -- E

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

I am unsure which parenthesis you are referring to escape

Now, there are not too many parentheses in the command. Howsoever, try

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

Hello RudiC

This has worked

thanks

1 Like