Visit Our UNIX and Linux User Community


SED extract XML value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED extract XML value
# 8  
Old 08-26-2009
Ok, new problem, ive got the following text file:


Code:
    <JDBCDataSource JNDIName="jdbc/rDB"
        Name="" PoolName="Othername"   Targets="myserver"/>
    <JDBCConnectionPool CapacityIncrement="2"
        DriverName="net.sourceforge.jtds.jdbc.Driver"
        InitialCapacity="4" MaxCapacity="20" Name="Othername"
        PasswordEncrypted="" Properties="user="
        Targets="myserver" TestConnectionsOnRelease="false" URL=""/>
    <JDBCDataSource JNDIName="jdbc/FirstDataBankDB" Name="fdbDataSource"
        PoolName="Othernamel" Targets="myserver"/>

<JDBCConnectionPool CapacityIncrement="5"
        DriverName="oracle.jdbc.driver.OracleDriver" InitialCapacity="5"
        MaxCapacity="35" Name="Othername"
        PasswordEncrypted=""
        PreparedStatementCacheSize="25"
        Properties=;dll=ocijdbc8;protocol=thin"
        RefreshMinutes="10" Targets="myserver"
        TestConnectionsOnRelease="false" TestConnectionsOnReserve="true"
        TestTableName="dual" URL=""/>

    <JDBCDataSource JNDIName="" 
        Name="ThisName"
        PoolName="l" RowPrefetchEnabled="true"
        RowPrefetchSize="100" Targets="myserver"
        CapacityIncrement="2"
        DriverName="net.sourceforge.jtds.jdbc.Driver"
        InitialCapacity="4" MaxCapacity="20" Name=""
        PasswordEncrypted="" Properties="user"
        Targets="myserver" TestConnectionsOnRelease="false" URL=""/>

    <JDBCDataSource JNDIName="jdbc/rDB"
        Name="" PoolName="Othername"   Targets="myserver"/>
    <JDBCConnectionPool CapacityIncrement="2"
        DriverName="net.sourceforge.jtds.jdbc.Driver"
        InitialCapacity="4" MaxCapacity="20" Name="Othername"
        PasswordEncrypted="" Properties="user="
        Targets="myserver" TestConnectionsOnRelease="false" URL=""/>
    <JDBCDataSource JNDIName="jdbc/FirstDataBankDB" Name="fdbDataSource"
        PoolName="Othernamel" Targets="myserver"/>
</Domain>

I need a SED function that isolates everything form "<JDBCDataSource" to "/>" where the Name parameter is "Name=ThisName". Ive tried modifying the range function given above, but I don't know how to make SED ignore the newline characters within a regular expression. The order of the entries is not guaranteed to be as listed and there can be many <JDBCDataSource entries.

---------- Post updated at 01:54 PM ---------- Previous update was at 01:24 PM ----------

I just thought of something. If I can get all of the entries on their own single line I could just grep for the "ThisName" parameter. Can anyone come up with a SED function that places everything in between "<" and "/>" on its own single line?

---------- Post updated at 02:57 PM ---------- Previous update was at 01:54 PM ----------

Or extract everything between the first occurrence of "<JDBCDataSource" before "Name=ThisName" and the first occurrence of "/>" after "Name=ThisName".

Last edited by ArterialTool; 08-26-2009 at 03:38 PM..
# 9  
Old 08-27-2009
Try this:

Code:
tr '\n' '_' < file | tr '>' '\n'|sed '/Name="ThisName"/d'| tr '\n' '>'| tr '_' '\n'


But more simply with awk:

Code:
awk '/ThisName/{next}1' ORS="\n\n" RS=  FS="\n" file

Regards
# 10  
Old 08-27-2009
Franklin52,

Neither solution seems to work, the AWK solution actually returns nothing.
# 11  
Old 08-27-2009
The solutions work fine for me. This is what I get:

Code:
$ cat file
    <JDBCDataSource JNDIName="jdbc/rDB"
        Name="" PoolName="Othername"   Targets="myserver"/>
    <JDBCConnectionPool CapacityIncrement="2"
        DriverName="net.sourceforge.jtds.jdbc.Driver"
        InitialCapacity="4" MaxCapacity="20" Name="Othername"
        PasswordEncrypted="" Properties="user="
        Targets="myserver" TestConnectionsOnRelease="false" URL=""/>
    <JDBCDataSource JNDIName="jdbc/FirstDataBankDB" Name="fdbDataSource"
        PoolName="Othernamel" Targets="myserver"/>

<JDBCConnectionPool CapacityIncrement="5"
        DriverName="oracle.jdbc.driver.OracleDriver" InitialCapacity="5"
        MaxCapacity="35" Name="Othername"
        PasswordEncrypted=""
        PreparedStatementCacheSize="25"
        Properties=;dll=ocijdbc8;protocol=thin"
        RefreshMinutes="10" Targets="myserver"
        TestConnectionsOnRelease="false" TestConnectionsOnReserve="true"
        TestTableName="dual" URL=""/>

    <JDBCDataSource JNDIName="" 
        Name="ThisName"
        PoolName="l" RowPrefetchEnabled="true"
        RowPrefetchSize="100" Targets="myserver"
        CapacityIncrement="2"
        DriverName="net.sourceforge.jtds.jdbc.Driver"
        InitialCapacity="4" MaxCapacity="20" Name=""
        PasswordEncrypted="" Properties="user"
        Targets="myserver" TestConnectionsOnRelease="false" URL=""/>

    <JDBCDataSource JNDIName="jdbc/rDB"
        Name="" PoolName="Othername"   Targets="myserver"/>
    <JDBCConnectionPool CapacityIncrement="2"
        DriverName="net.sourceforge.jtds.jdbc.Driver"
        InitialCapacity="4" MaxCapacity="20" Name="Othername"
        PasswordEncrypted="" Properties="user="
        Targets="myserver" TestConnectionsOnRelease="false" URL=""/>
    <JDBCDataSource JNDIName="jdbc/FirstDataBankDB" Name="fdbDataSource"
        PoolName="Othernamel" Targets="myserver"/>
</Domain>
$
$ tr '\n' '_' < file | tr '>' '\n'|sed '/Name="ThisName"/d'| tr '\n' '>'| tr '_' '\n'
    <JDBCDataSource JNDIName="jdbc/rDB"
        Name="" PoolName="Othername"   Targets="myserver"/>
    <JDBCConnectionPool CapacityIncrement="2"
        DriverName="net.sourceforge.jtds.jdbc.Driver"
        InitialCapacity="4" MaxCapacity="20" Name="Othername"
        PasswordEncrypted="" Properties="user="
        Targets="myserver" TestConnectionsOnRelease="false" URL=""/>
    <JDBCDataSource JNDIName="jdbc/FirstDataBankDB" Name="fdbDataSource"
        PoolName="Othernamel" Targets="myserver"/>

<JDBCConnectionPool CapacityIncrement="5"
        DriverName="oracle.jdbc.driver.OracleDriver" InitialCapacity="5"
        MaxCapacity="35" Name="Othername"
        PasswordEncrypted=""
        PreparedStatementCacheSize="25"
        Properties=;dll=ocijdbc8;protocol=thin"
        RefreshMinutes="10" Targets="myserver"
        TestConnectionsOnRelease="false" TestConnectionsOnReserve="true"
        TestTableName="dual" URL=""/>

    <JDBCDataSource JNDIName="jdbc/rDB"
        Name="" PoolName="Othername"   Targets="myserver"/>
    <JDBCConnectionPool CapacityIncrement="2"
        DriverName="net.sourceforge.jtds.jdbc.Driver"
        InitialCapacity="4" MaxCapacity="20" Name="Othername"
        PasswordEncrypted="" Properties="user="
        Targets="myserver" TestConnectionsOnRelease="false" URL=""/>
    <JDBCDataSource JNDIName="jdbc/FirstDataBankDB" Name="fdbDataSource"
        PoolName="Othernamel" Targets="myserver"/>
$
$
$ awk '/ThisName/{next}1' ORS="\n\n" RS=  FS="\n" file
    <JDBCDataSource JNDIName="jdbc/rDB"
        Name="" PoolName="Othername"   Targets="myserver"/>
    <JDBCConnectionPool CapacityIncrement="2"
        DriverName="net.sourceforge.jtds.jdbc.Driver"
        InitialCapacity="4" MaxCapacity="20" Name="Othername"
        PasswordEncrypted="" Properties="user="
        Targets="myserver" TestConnectionsOnRelease="false" URL=""/>
    <JDBCDataSource JNDIName="jdbc/FirstDataBankDB" Name="fdbDataSource"
        PoolName="Othernamel" Targets="myserver"/>

<JDBCConnectionPool CapacityIncrement="5"
        DriverName="oracle.jdbc.driver.OracleDriver" InitialCapacity="5"
        MaxCapacity="35" Name="Othername"
        PasswordEncrypted=""
        PreparedStatementCacheSize="25"
        Properties=;dll=ocijdbc8;protocol=thin"
        RefreshMinutes="10" Targets="myserver"
        TestConnectionsOnRelease="false" TestConnectionsOnReserve="true"
        TestTableName="dual" URL=""/>

    <JDBCDataSource JNDIName="jdbc/rDB"
        Name="" PoolName="Othername"   Targets="myserver"/>
    <JDBCConnectionPool CapacityIncrement="2"
        DriverName="net.sourceforge.jtds.jdbc.Driver"
        InitialCapacity="4" MaxCapacity="20" Name="Othername"
        PasswordEncrypted="" Properties="user="
        Targets="myserver" TestConnectionsOnRelease="false" URL=""/>
    <JDBCDataSource JNDIName="jdbc/FirstDataBankDB" Name="fdbDataSource"
        PoolName="Othernamel" Targets="myserver"/>
</Domain>
$
$

Am I missing something?
# 12  
Old 08-27-2009
I think your actually getting the opposite of what I am trying to get. I came up with this solution that seems to work:

Code:
cat file | awk '{printf("%s", $0); if ( $0 ~ /.*\/>/ ) {printf("\n");}}' | grep ThisName | tr -d '\n'

# 13  
Old 08-27-2009
Quote:
Originally Posted by ArterialTool
I think your actually getting the opposite of what I am trying to get. I came up with this solution that seems to work:

Code:
cat file | awk '{printf("%s", $0); if ( $0 ~ /.*\/>/ ) {printf("\n");}}' | grep ThisName | tr -d '\n'

Sorry, I misread the question but this should get the part you have colored:

Code:
awk '/ThisName/' ORS="\n\n" RS=  FS="\n" file

Regards
# 14  
Old 08-27-2009
ArterialTool, you really should consider using an XSL aware tool such as xsltproc for this type of work.

For example here is how you can use xsltproc to solve your original question
Code:
$ var=`xsltproc -param which "'min-pool-size'" file.xsl file.xml`
$ echo $var
2
$

where file.xml is your input file and file.xsl is
Code:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <!-- pass in as -param which "'value'"  -->
   <xsl;param name=which" />

    <xsl:output method="text'/>

   <xsl:template match="/">
      <xsl:apply-templates/>
   </xsl:template>

   <!-- output nodes that match -->
   <xsl:template match="*" priority="1">
      <xsl:if test="name(.)=$which">
        <xsl:value-of select="."/>
      </xsl:if>
   </xsl:template>

   <!-- eat all other output -->
   <xsl:template match="*|text()" priority="0"/>

</xsl:stylesheet>


Previous Thread | Next Thread
Test Your Knowledge in Computers #276
Difficulty: Easy
OpenSolaris was based on Solaris, which was originally released by Sun in 1999.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract a value from an xml file

I have this XML file format and all in one line: Fri Dec 23 00:14:52 2016 Logged Message:689|<?xml version="1.0" encoding="UTF-8"?><PORT_RESPONSE><HEADER><ORIGINATOR>XMG</ORIGINATOR><DESTINAT... (16 Replies)
Discussion started by: mrn6430
16 Replies

2. Shell Programming and Scripting

Extract strings from XML files and create a new XML

Hello everybody, I have a double mission with some XML files, which is pretty challenging for my actual beginner UNIX knowledge. I need to extract some strings from multiple XML files and create a new XML file with the searched strings.. The original XML files contain the source code for... (12 Replies)
Discussion started by: milano.churchil
12 Replies

3. Shell Programming and Scripting

Extract a particular xml only from an xml jar file

Hi..need help on how to extract a particular xml file only from an xml jar file... thanks! (2 Replies)
Discussion started by: qwerty000
2 Replies

4. Shell Programming and Scripting

sed - extract text from xml file

hi, please help, i have an xml file, e.g: ... <tag> test text asdas="${abc}" xvxvbs:asdas${222}sdad asasa="${aa_bb_22}" </tag> ... i want to extract all "${...}", e.g: ${abc} ${222} ${aa_bb_22} thank you. (2 Replies)
Discussion started by: gioni
2 Replies

5. Shell Programming and Scripting

Extract Multivalue from XML

I have below attached xml file , how can I have my desired output as below. i/p file <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:executeMDXResponse... (4 Replies)
Discussion started by: manas_ranjan
4 Replies

6. Shell Programming and Scripting

Extract value from XML

I have a file like below <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:executeMDXResponse... (9 Replies)
Discussion started by: manas_ranjan
9 Replies

7. Shell Programming and Scripting

sed extract from xml

I have an xml file that generally looks like this: "<row><dnorpattern>02788920</dnorpattern><description/></row><row><dnorpattern>\+ 44146322XXXX</dnorpattern><description/></row><row><dnorpattern>40XXX</dnorpattern><description/></row><row><dnorpattern>11</dn... (4 Replies)
Discussion started by: garboon
4 Replies

8. Shell Programming and Scripting

Using SED/AWK to extract xml at end of file

Hello everyone, Firstly i do not require alot of help.. i am right at the end of finishing my scipt but cannot find a solution to the last part. What i need to do is, prompt the user for a file to work with, which i have done. promt the user for an output file - which is done. #!/bin/bash... (14 Replies)
Discussion started by: hugh86
14 Replies

9. Shell Programming and Scripting

sed or awk to extract data from Xml file

Hi, I want to get data from Xml file by using sed or awk command. I want to get the following result : mon titre 1;Createur1;Dossier1 mon titre 1;Createur1;Dossier1 and save it in cvs file (fichier.cvs). FROM this Xml file (test.xml): <playlist version="1"> <trackList> <track>... (1 Reply)
Discussion started by: yeclota
1 Replies

10. Shell Programming and Scripting

simerian XML Extract

The script following in this thread allows XML data to be located and extracted in a variety of forms from an XML data stream. Using this utility, it is possible to extract all manner of XML subsets and allow data to be post inserted into the "original" XML at any logical point. The pipe is... (2 Replies)
Discussion started by: Simerian
2 Replies

Featured Tech Videos