Parsing xml file using Sed


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Parsing xml file using Sed
# 1  
Old 04-08-2008
Data Parsing xml file using Sed

Hi All,

I have this(.xml) file as:
Code:
 <!-- define your instance here -->
   <instance name='ins_C2Londondev' user='' group='' fullname='B2%20-%20London%20(dev)' >
      <property>
      </property>
   </instance>

I want output as:
Code:
<!-- define your instance here -->
   <instance name='ins_B2Londondev' user='C2_dev' group='sports' fullname='B2%20-%20London%20(dev)' >
            <property>
      </property>
   </instance>

I want to do this using Sed command.Please help as this is vey urgent.

Thanks in advance

Last edited by Yogesh Sawant; 04-08-2008 at 05:39 AM.. Reason: added code tags
# 2  
Old 04-08-2008
Perhaps you could elaborate on your requirements. If all you need is to change name='ins_C2Londondev' to name='ins_B2Londondev', change user='' to user='C2_dev', change group='' to group='sports' regardless of context, then you don't really need any XML parsing. Oh, and if the new values somehow depend on the input, we can't guess what they need to be, so you need to explain that. On the other hand, if you need real XML parsing, sed is not the right tool.

Code:
sed -e "s/name='ins_C2Londondev'/name='ins_B2Londondev'/" \
    -e "s/user=''/user='C2_dev'/" -e "s/group=''/group='sports'/" file.xml

# 3  
Old 04-08-2008
First of all thx for your time.This is the complete .xml file:<name>B2 - London</name>
<description>Calculates risk numbers for the global SCT desk and london credit flow desk</description>
<version>1.0</version>
<revision></revision>
<revcomment></revcomment>
<bline>Fixed Income</bline>
<userid>kkinha</userid>

<!-- define your global properties here -->
<property>
<SRC_PATH>/home/B2_dev/grid/B2_RiskEngine_Grid/OvernightLondon/VB2.STABLE</SRC_PATH>
</property>

<!-- define your instance here -->
<instance name='ins_B2Londondev' user='' group='' fullname='B2%20-%20London%20(dev)' >
<!-- ins_B2Londondev = B2 - London (dev) -->
<!-- define your instance specific properties here -->
<property>
</property>
</instance>
<instance name='ins_B2Londonprod' user='' group='' fullname='B2%20-%20London%20(prod)' >
<!-- ins_B2Londonprod = B2 - London (prod) -->
<!-- define your instance specific properties here -->
<property>
</property>
</instance>
<instance name='ins_B2LondonInt' user='' group='' fullname='B2%20-%20London%20(Stage)' >
<!-- ins_B2LondonInt = B2 - London (Stage) -->
<!-- define your instance specific properties here -->
<property>
</property>
</instance>

<component name='Undefined'>
<version>1.0</version>

<os>Red Hat Enterprise Linux AS 4</os>

<os>SunOS 5.8</os>

<package>B2_GridComponentInstall</package>
</component>

<package name='B2_GridComponentInstall'>
<description>Package for deploying B2 - London</description>
<version>1.0</version>

<dir src='${SRC_PATH}' dst='/home/${user}' owner='${user}' group='${group}' type='' exclude='' perm='755' option='' />
<post>${SRC_PATH}/deployOvernightRiskEngine.bat</post>
<auto_home key='${user}' path='/local/0/home/${user}' server='localhost' />


</package>

</application>


I have to change user='' to user='C2_Dev' , group='' to group='sports' and dst='/home/${user} to dst=$DST_PATH.Where DST_PATH is shell script variable.User and group shd be changed depending on user input i.e instance name for ex. if instance name is ins_B2Londondev then user and group for that instance shd be changed.I am doin like this buts it's giving error:
sed -n \
"/${Instance}/p"\
-e '/instance name/p'\
-e "s/user=''/user='B2_dev'/" -e "s/group=''/group='games'/" B2GridTemplate.xml

Error::Can't open -e
Can't open /instance name/p
Can't open -e
Can't open s/user=''/user='B2_dev'/
Can't open -e
Can't open s/group=''/group='games'/
<instance name='ins_B2Londondev' user='' group='' fullname='B2%20-%20London%20(dev)' >
<!-- ins_B2Londondev = B2 - London (dev) -->

Please help!!!!
# 4  
Old 04-08-2008
Try add another -e after the first -n. I think it requires one for each script snippet, if you have several.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with parsing xml file

Hi, Need help with parsing xml data in unix and place it in a csv file. My xml file looks like this: <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <iwgroups> <nextid value="128"> </nextid> <iwgroup name="RXapproval" id="124" display-name="RXapproval"... (11 Replies)
Discussion started by: ajayakunuri
11 Replies

2. UNIX for Dummies Questions & Answers

Parsing XML file

I want to parse xml file sample file....... <name locale="en">my_name<>/name><lastChanged>somedate</lastChanged><some more code here> <name locale="en">tablename1<>/name><lastChanged>somedate</lastChanged> <definition><dbquery><sources><sql type="cognos">select * from... (10 Replies)
Discussion started by: ms2001
10 Replies

3. Shell Programming and Scripting

XML: parsing of the Google contacts XML file

I am trying to parse the XML Google contact file using tools like xmllint and I even dived into the XSL Style Sheets using xsltproc but I get nowhere. I can not supply any sample file as it contains private data but you can download your own contacts using this script: #!/bin/sh # imports... (9 Replies)
Discussion started by: ripat
9 Replies

4. Shell Programming and Scripting

Parsing an XML file

Hello, I have the following xml file as an input. <?xml version="1.0" encoding="UTF-8"?> <RECORDS PS3_VERSION="1104_01"><RECORD> <POI_ID>931</POI_ID> <SUPPLIER_ID>2</SUPPLIER_ID> <POI_PVID>997920846</POI_PVID> <DB_ID>1366650925</DB_ID> <REGION>H1</REGION> <POI_NAME NAME_TYPE="Official"... (4 Replies)
Discussion started by: ramky79
4 Replies

5. Shell Programming and Scripting

parsing xml file

Hello! We need to parse weblogic config.xml file and display rows in format: machine:listen-port:name:application_name In our enviroment the output should be (one line for every instance): Crm-Test-Web:8001:PIA:peoplesoft Crm-Test-Web:8011:PIA:peoplesoft... (9 Replies)
Discussion started by: annar
9 Replies

6. Shell Programming and Scripting

Help in parsing xml file (sed/nawk)

I have a large xml file as shown below: <input> <blah> <blah> <atr="blah blah value = ""> <blah> <blah> </input> ..2nd chunk... ..3rd chunk... ...4th chunk... All lines between <input> and </input> is one 'order' and this 'order' is repeated... (14 Replies)
Discussion started by: shekhar2010us
14 Replies

7. Shell Programming and Scripting

Parsing xml file

hi guys, great help to the original question, can i expand please? i have large files filled with blocks like this <Placemark> network type: hot line1 line2 line3 <styleUrl>red.png</styleUrl> </Placemark> <Placemark> network type: cold line1 line2 line3... (3 Replies)
Discussion started by: garvald
3 Replies

8. UNIX for Dummies Questions & Answers

Help parsing a XML file ....

Well I have read several threads on the subject ... but being a newbie like me makes it hard to understand ... What I need is the following: Input data: ------- snip --------- <FavouriteLocations> <FavouriteLocations class="FavouriteList"><Item... (6 Replies)
Discussion started by: misak
6 Replies

9. Shell Programming and Scripting

parsing xml with awk/sed

Hi people!, I need extract from the file (test-file.txt) the values between <context> and </context> tag's , the total are 7 lines,but i can only get 5 or 2 lines!!:confused: Please look my code: #awk '/context/{flag=1} /\/context/{flag=0} !/context/{ if (flag==1) p rint $0; }'... (3 Replies)
Discussion started by: ricgamch
3 Replies

10. Shell Programming and Scripting

can i do XML parsing usind sed

Hi all... I want to parse a xml filein unix .. Can i use SED or unix script to parse the xml file .. If so can anyone show a sample script that will parse the xml file .. Thanks in advance, Arun ,,,, (3 Replies)
Discussion started by: arunkumar_mca
3 Replies
Login or Register to Ask a Question