Shell Script to read XML file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script to read XML file
# 1  
Old 04-02-2013
Linux Shell Script to read XML file

Hi unix Gurus,
I am really new to Unix Scripting. Please help me to create a shell script which reads the xml file and from that i need to fetch a particular information.

For example
Code:
<SOURCE BUSINESSNAME ="" DATABASETYPE ="Teradata" DBDNAME ="DWPROD3" DESCRIPTION ="" NAME ="ACTRL_BNFT_KEY_DMN" OBJECTVERSION ="1" OWNERNAME ="COC_V20_ETL_APPL" VERSIONNUMBER ="1">
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" DESCRIPTION ="" FIELDNUMBER ="1" FIELDPROPERTY ="0" FIELDTYPE ="ELEMITEM" HIDDEN ="NO" </SOURCE>


From the above xml file , I have to read the xml file and get the source name from the file as the below output.
Code:
SOURCE ->ACTRL_BNFT_KEY_DMN

.

Please help me UNIX Gurus.

Last edited by Franklin52; 04-02-2013 at 08:43 AM.. Reason: Please use code tags
# 2  
Old 04-02-2013
One way:

Code:
$ sed -n '/SOURCE.* NAME /s/.* NAME ="\([^"]*\)".*/SOURCE->\1/p' file
SOURCE->ACTRL_BNFT_KEY_DMN

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 04-02-2013
Thanks Guru.

Can I use something like the below, where it initially read the file and passes the output to sed command.

cat filename | sed -n '/SOURCE.* NAME /s/.* NAME ="\([^"]*\)".*/SOURCE->\1/p' file
# 4  
Old 04-02-2013
Output of cat being piped to sed is not a good way of doing. Though the output will be same, its inefficient because sed can read file on its own, and hence using cat is actually an overhead. Read this for more.

Guru.
# 5  
Old 04-03-2013
Thanks for your reply Guru,

but i have a concern. In my xml file where ever i have "NAME =" its looking for it and getting the corresponding value. But i want a particular value only and not all the Names.

To be clear

<SOURCE BUSINESSNAME ="" DATABASETYPE ="Teradata" DBDNAME ="DWPROD3" DESCRIPTION ="" NAME ="ACTRL_BNFT_KEY_DMN_SRC" OBJECTVERSION ="1" OWNERNAME ="COC_V20_ETL_APPL" VERSIONNUMBER ="1">
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" DESCRIPTION ="" FIELDNUMBER ="1" FIELDPROPERTY ="0" FIELDTYPE ="ELEMITEM" HIDDEN ="NO" KEYTYPE ="PRIMARY KEY" LENGTH ="0" LEVEL ="0" NAME ="ACTRL_BNFT_KEY_ID" NULLABLE ="NOTNULL" OCCURS ="0" OFFSET ="0" PHYSICALLENGTH ="25" PHYSICALOFFSET ="0" PICTURETEXT ="" PRECISION ="25" SCALE ="0" USAGE_FLAGS =""/>

I want to get only the name of the source which ends with _SRC which i have highlighted.

Thanks Again
# 6  
Old 04-03-2013
Code:
sed -n '/SOURCE.* NAME /s/.* NAME ="\([^"]*_SRC\)".*/SOURCE->\1/p' file

Guru.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need get data from XML file through shell script..

hi all, here is the sample log file and these errors are repeated in log file.. i need all the repeated time stamp ,severity and message tags needs to print in output file.. through shell script <log-message> <timestamp>2019-03-13T04:52:49.648-05:00</timestamp> <severity>ERROR</severity>... (17 Replies)
Discussion started by: ravi
17 Replies

2. UNIX for Beginners Questions & Answers

UNIX shell script required to read two columns from xml

Hello All, I am new to unix scripting. I need to read FROMINSTANCE, FROMFIELD from below XML code and need to write a script for insert into SQ_EPIC values( "DID", "PROJECT_NAME") Select DID, PROJECT_NAME from EPIC<CONNECTOR TOINSTANCETYPE="Source Qualifier" TOINSTANCE="SQ_EPIC" TOFIELD="DID"... (7 Replies)
Discussion started by: sekhar.lsb
7 Replies

3. Shell Programming and Scripting

Read xml tags and then remove the tag using shell script

<Start> <Header> This is header section </Header> <Body> <Body_start> This is body section <a> <b> <c> <st>111</st> </c> <d> <st>blank</st> </d> </b> </a> </Body_start> <Body_section> This is body section (3 Replies)
Discussion started by: RJG
3 Replies

4. Shell Programming and Scripting

Read xml file till script finds separation and run again for next input and so on

Hi All, I have one query, I managed to run script with user inputs through command line or with 1 file. But I need to read a txt file/xml file in which user can mention multiple sets of answers and script should run for each set till it reach the EOF. Thanks in advance for example, the file... (3 Replies)
Discussion started by: rv_champ
3 Replies

5. Red Hat

How to read an xml file through shell script?

Hey , can we read an xml file and make changes in it through shell script. Thanks (4 Replies)
Discussion started by: ramsavi
4 Replies

6. UNIX for Advanced & Expert Users

Shell Script to read XML tags and the data within that tag

Hi unix Gurus, I am really new to Unix Scripting. Please help me to create a shell script which reads the xml file and from that i need to fetch a particular information. For example <SOURCE BUSINESSNAME ="" DATABASETYPE ="Teradata" DBDNAME ="DWPROD3" DESCRIPTION ="" NAME... (2 Replies)
Discussion started by: SmilePlease
2 Replies

7. Shell Programming and Scripting

script to read XML file

Dear All, I have one log file and it contains lot of XML as below. ... (1 Reply)
Discussion started by: murtujak
1 Replies

8. Shell Programming and Scripting

Unix Script to read the XML file from Website

Hi Experts, I need a unix shell script which can copy the xml file from the below pasted website and paste in in my unix directory. http://www.westpac.co.nz/olcontent/olcontent.nsf/fx.xml Thanks in Advance... (8 Replies)
Discussion started by: phani333
8 Replies

9. Shell Programming and Scripting

How to remove xml namespace from xml file using shell script?

I have an xml file: <AutoData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Table1> <Data1 10 </Data1> <Data2 20 </Data2> <Data3 40 </Data3> <Table1> </AutoData> and I have to remove the portion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" only. I tried using sed... (10 Replies)
Discussion started by: Gary1978
10 Replies

10. UNIX for Advanced & Expert Users

Shell script failing to read large Xml record-urgent critical help

Hi All, I have shell script running on AIX 5.3 box. It has 7 to 8 "sed" commands piped(|) together. It has a an Xml file as its input which has many records internally. There are certain record which which have more than hundered tags.The script is taking a huge amount of time more than 1.5 hrs... (10 Replies)
Discussion started by: aixjadoo
10 Replies
Login or Register to Ask a Question