2014-02-18 103 views
0

我不是很擅長匆忙,所以我需要一隻手。Bash從xml解析數據

我有以下的輸出,我想分析數據來源:

實際輸出:

<connection-pool name="name1" max-connections="50" min-connections="5"> 
<connection-factory factory-class="oracle.jdbc.pool.OracleDataSource" user="user1" password="xxxx" url="jdbc:oracle:thin:@server1.domain.com:1550:name1" commit-record-table-name=""> 
<connection-pool name="name2" max-connections="50" min-connections="5"> 
<connection-factory factory-class="oracle.jdbc.pool.OracleDataSource" user="user2" password="xxxx" url="jdbc:oracle:thin:@server2.domain.com:1524:name2" commit-record-table-name=""> 
<connection-pool name="name3" max-connections="15" min-connections="5"> 
<connection-factory factory-class="oracle.jdbc.pool.OracleDataSource" user="user3" password="xxxx" url="jdbc:oracle:thin:@server3.domain.com:1528:name3" commit-record-table-name=""> 
<connection-pool name="name4" initial-limit="1" max-connections="10" min-connections="1" num-cached-statements="5"> 
<connection-factory factory-class="oracle.jdbc.pool.OracleDataSource" user="user4" password="xxxx" url="jdbc:oracle:thin:@server4.domain.com:1538:name4" commit-record-table-name=""/> 
<connection-pool name="name5" initial-limit="1" max-connections="10" min-connections="1" num-cached-statements="5"> 
<connection-factory factory-class="oracle.jdbc.pool.OracleDataSource" user="user5" password="xxxx" url="jdbc:oracle:thin:@//server5.domain.com:1537/name5"/> 

所需的輸出:

name="name1" max-connections="50" min-connections="5" url="jdbc:oracle:thin:@server1.domain.com:1550:name1" 
name="name2" max-connections="50" min-connections="5" url="jdbc:oracle:thin:@server2.domain.com:1524:name2" 
name="name3" max-connections="15" min-connections="5" url="jdbc:oracle:thin:@server3.domain.com:1528:name3" 
name="name4" max-connections="10" min-connections="1" url="jdbc:oracle:thin:@server4.domain.com:1538:name4" 
name="name5" max-connections="10" min-connections="1" url="jdbc:oracle:thin:@//server5.domain.com:1537/name5" 

我將不勝感激,如果有人可以幫助我這,順便說一句,它應該在bash中完成,因爲我不能在所有生產服務器上安裝軟件..

在此先感謝!

+0

做你的生產服務器** **真的已經不使用Python出貨,或任何其他語言,XML在標準庫中的支持?使用bash解析XML需要第三方工具 - xmlstarlet,xmllint等。 –

+0

請看這個答案:http://stackoverflow.com/a/13463365/1050015 – Carlo

+0

@Carlo,因爲使用第三方工具,問題表明「我無法在生產服務器上安裝軟件」... –

回答

1

因爲這不是有效的XML,還不如用SED:

sed -n ' 
    /<connection-pool/ {s///; s/\/\?>$//; s/ \(initial-limit\|num-cached-statements\)="[^"]*"//g; p} 
    /<connection-factory/ {s///; s/\/\?>$//; s/ \(factory-class\|user\|password\|commit-record-table-name\)="[^"]*"//g; p} 
' connections.log | paste -d "" - - 
name="name1" max-connections="50" min-connections="5" url="jdbc:oracle:thin:@server1.domain.com:1550:name1" 
name="name2" max-connections="50" min-connections="5" url="jdbc:oracle:thin:@server2.domain.com:1524:name2" 
name="name3" max-connections="15" min-connections="5" url="jdbc:oracle:thin:@server3.domain.com:1528:name3" 
name="name4" max-connections="10" min-connections="1" url="jdbc:oracle:thin:@server4.domain.com:1538:name4" 
name="name5" max-connections="10" min-connections="1" url="jdbc:oracle:thin:@//server5.domain.com:1537/name5" 
+0

非常感謝,它輸出我所需要的! –