UNIX Shell script to work with .xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX Shell script to work with .xml file
# 1  
Old 05-17-2014
RedHat UNIX Shell script to work with .xml file

Hi Team,

Could you please help me on below query:
I want to retrieve XML elements from one .xml file. This .xml file has commented tags as well. so i am planning to write Unix command/script which 1.will chekc for this .xml file
2. it will ignore the commented XML lines. i.e. XML tags between
<!-- and -->
3. and it will check for uncommented tags and will retrieve the values as below: and i need to check for the uncommented tags individually and there are duplicate tags with different values.
(I am providing belwo my XML content as below)
Please help to advise.

Required output:
----------------
Code:
port="8080" 
protocol="HTTP/1.1"
connectionTimeout="300000"
redirectPort="8443" 
maxThreads="200" 
minSpareThreads="4"        
maxSpareThreads="50" 
maxKeepAliveRequests="1" 

port="8009" 
protocol="AJP/1.3" 
redirectPort="8443"

name="jdbc/CLH" 
auth="Container" 
type="javax.sql.DataSource"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
initialSize="10" 
maxActive="100" 
maxIdle="20" 
maxWait="10000"
validationQuery="select 1"
testOnBorrow="true"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"
url="jdbc:jtds:sqlserver://xx.xxx.xx.xx:xxxx/Chase"
username="xxxxx" 
password="xxxxxx"

---------------
XML file has content as below:

Code:
<!-- SingleSignOn valve, share authentication between web applications
            Documentation at: /docs/config/valve.html -->
     <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->
     <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
    
     <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="300000"
               redirectPort="8443" maxThreads="200" minSpareThreads="4"        maxSpareThreads="50" maxKeepAliveRequests="1" />

    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

    <Resource name="jdbc/CLH" auth="Container" type="javax.sql.DataSource"
              driverClassName="net.sourceforge.jtds.jdbc.Driver"
              initialSize="10" maxActive="100" maxIdle="20" maxWait="10000"
              validationQuery="select 1"
              testOnBorrow="true"
              factory="org.apache.commons.dbcp.BasicDataSourceFactory"
              url="jdbc:jtds:sqlserver://xx.xxx.xx.xx:xxxx/Chase"
              username="xxxxx" password="xxxxxx"  />

    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />

<Resource name="jdbc/MINT" auth="Container" type="javax.sql.DataSource"
               maxActive="3" maxIdle="1" maxWait="10000"
               username="XXX" password="XXX" driverClassName="oracle.jdbc.OracleDriver"
               url="jdbc:oracle:thin:@XXXX:XXX:XXX"
               factory="org.apache.commons.dbcp.BasicDataSourceFactory"/>

-----------------------------
Please could you help to advise.

Thanks..

Last edited by vgersh99; 05-17-2014 at 01:23 PM.. Reason: code tags, please!
# 2  
Old 05-17-2014
Welcome to the forum.
You can try something like ..

Code:
awk '{for (i=1;i<=NF;i++) {if ($i ~ /=/) {print $i}}}' file

While there are many ways to extract fields from an xml file. The best way for a long term solution, I would prefer to use a good xml parser tool.
Thanks.


EDIT : As pilnet101 pointed out, this wont work for the tags with spaces. I didn't observe in hurry that they also contain spaces.

Last edited by clx; 05-18-2014 at 01:18 AM.. Reason: incomplete solution
# 3  
Old 05-17-2014
The above post has a few bugs (i.e. Description variable will not show).

This is messy and probably can be condensed a lot, but it was fun to create Smilie. Hope it helps:

Code:
awk 'BEGIN{ORS=" "}1' xml|sed -r 's/<!--[^-]+-->//g'|awk '{gsub(/\/>/,"\n",$0)}1'|awk '{$1=""}1'|sed -r 's/\w+="[^"]+"/&uniQue/g'|awk 'BEGIN{RS="uniQue"}1'|sed 's/^ //g'

This User Gave Thanks to pilnet101 For This Post:
# 4  
Old 05-18-2014
HI clx and pilnet101,

Thank you for your quick response. These commands are helpful...
I will try to modify as i need the output.

Thanks..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

How to pass the parameter in xml file in UNIX shell script?

Hi, I have an XML file like the following... <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ONDEMAND_JOB VERSION="5.1" LOCALE="en_US"> <IMPORT_JOBSET TC_CONNECTION_NAME="default" ENVIRONMENT="PRD" USERNAME="Administrator" PASSWORD="AdminPassword" CALENDAR="Main Monthly Calendar"... (3 Replies)
Discussion started by: Debalina Roy
3 Replies

3. Shell Programming and Scripting

How to pass the parameter in xml file in UNIX shell script?

Hi, I have an XML file like the following... <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ONDEMAND_JOB VERSION="5.1" LOCALE="en_US"> <IMPORT_JOBSET TC_CONNECTION_NAME="default" ENVIRONMENT="PRD" USERNAME="Administrator" PASSWORD="AdminPassword" CALENDAR="Main Monthly Calendar"... (2 Replies)
Discussion started by: Debalina Roy
2 Replies

4. 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

5. Shell Programming and Scripting

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 <SOURCE BUSINESSNAME ="" DATABASETYPE ="Teradata" DBDNAME ="DWPROD3" DESCRIPTION ="" NAME... (5 Replies)
Discussion started by: SmilePlease
5 Replies

6. Shell Programming and Scripting

Shell Script Xml file wallpaper

Hello, I have a custom background in my Fedora 14 OS. which changes every 10 mins, I was able to edit an Xml file for this purpose. The structure of the Xml file looks as follows : <!-- This animation will start at time shown above. --> <static> <duration>600.0</duration> ... (3 Replies)
Discussion started by: ajayram
3 Replies

7. 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

8. Shell Programming and Scripting

i want to nvoke a xml file from a shell script

hi all , i have a shell script that performs all the steps required for deployment of a tar file .. there is a tool that helps me in finding out what files should go in that tar file... this tool is fully in xml as it invokes ant ( a build tool in java ).... i want to invoke that xml file from a... (1 Reply)
Discussion started by: sais
1 Replies

9. UNIX for Dummies Questions & Answers

need to invoke a shell script from xml file.

hi all, forgive me if this question is not relevant to this section. i have a shell file that will automatically deploy the files that are necessary and will compile them. i need to automate this management process through cruise control, for this i need to know whether it is possible for me to... (2 Replies)
Discussion started by: sais
2 Replies

10. Shell Programming and Scripting

Converting Text File into XML using Unix Shell Scripts

Hi everyone, If someone out there could help me out with this problem. I would really appreciate it. I am trying to convert a file into xml format using Unix shell scripts. The file has fields with each field having a certain number of bytes, but the fields are not delimited by anything... (10 Replies)
Discussion started by: Laud12345
10 Replies
Login or Register to Ask a Question