2016-10-20 70 views
-2

想要通過bash shell腳本編輯XML文件。 我被困在尋找解決方案 - 請分享您的建議,如果這可以通過bash shell腳本解決。從bash shell編輯XML文件

輸入文件(將存儲在服務器):

<?xml version="1.0" encoding="UTF-8"?> 
 
<properties> 
 
\t <directories installDir="/fs0/"> 
 
\t \t <directoriesInstance id="sharedDir" path=""/> 
 
\t \t <directoriesInstance id="loaderInput" path="/fs0/share/iad/input"/> 
 
\t \t <directoriesInstance id="loaderProcessing" path="/fs0/share/iad/processing"/> 
 
\t \t <directoriesInstance id="loaderError" path="/fs0/share/iad/error"/> 
 
\t \t <directoriesInstance id="loaderCompleted" path="/fs0/share/iad/completed"/> 
 
\t </directories> 
 
    
 
    <applicationServerInstance id="app" serviceName="App Server" rmiPort="15001" jvmParameters="-Xmx3072m -Xms512m -XX:-UseGCOverheadLimit -XX:MaxPermSize=196m -Dsun.lang.ClassLoader.allowArraySyntax=true -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/fs0/clarity1/clarity/logs -Xloggc:/fs0/clarity1/clarity/logs/app_gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Dcustom.properties=/fs0/share/custom.properties" maxThreads="1000" programParameters="" distributed="false" runJobScheduler="false" useSSO="true" maxConcurrentJobs="10" runProcessEngine="false" messageTimeToLive="120" messageReceiverInterval="5" exceptionRunInterval="normal" maxXmlNodesLimit="150000"/> 
 
    <applicationServer> 
 
\t \t <applicationServerInstance id="app" serviceName=" App Server" rmiPort="15001" jvmParameters="-Xmx3072m -Xms512m -XX:-UseGCOverheadLimit -XX:MaxPermSize=196m -Dsun.lang.ClassLoader.allowArraySyntax=true -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/fs0/clarity1/clarity/logs -Xloggc:/fs0/clarity1/clarity/logs/app_gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Dcustom.properties=/fs0/clarity1/share/custom.properties" maxThreads="1000" programParameters="" distributed="false" runJobScheduler="false" useSSO="true" maxConcurrentJobs="10" runProcessEngine="false" messageTimeToLive="120" messageReceiverInterval="5" exceptionRunInterval="normal" maxXmlNodesLimit="150000"/> 
 
\t </applicationServer> 
 

 
</properties>

在上面的代碼中,我想尋找applicationServerInstance標籤,看看它是否有Dcustom.properties =/FS0 /清晰度1 /共享/ custom.properties jvmproperties標記 - 如果它不存在我想添加到文件並保存文件。 所有應該在bash shell腳本中。

我想-Dcustom.properties =/FS0 /股/ custom.properties值添加到jvmParameters屬性在applicationServerInstance標籤,如果不存在的話。在上面的例子中它有這個值,但是如果它不存在,我會添加這個-Dcustom.properties =/fs0/share/custom.properties值。

例如說我應該-Dcustom.properties =/FS0 /共享/ custom.properties值添加到jvmParameters屬性在applicationServerInstance標籤到下面片段。

<applicationServerInstance id="app" serviceName=" App Server" rmiPort="15001" jvmParameters="-Xmx3072m -Xms512m -XX:-UseGCOverheadLimit -XX:MaxPermSize=196m -Dsun.lang.ClassLoader.allowArraySyntax=true -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/fs0/clarity1/clarity/logs -Xloggc:/fs0/clarity1/clarity/logs/app_gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC " maxThreads="1000" programParameters="" distributed="false" runJobScheduler="false" useSSO="true" maxConcurrentJobs="10" runProcessEngine="false" messageTimeToLive="120" messageReceiverInterval="5" exceptionRunInterval="normal" maxXmlNodesLimit="150000"/>

+0

沒有'jvmproperties' *標記*,但該值包含在'jvmParameters' *屬性*中。你想添加值,還是替換現有的'custom.properties'? – choroba

+0

我想爲** jvmParameters **屬性添加** - Dcustom.properties =/fs0/share/custom.properties **值在** applicationServerInstance **標記中(如果它不存在)。在上面的例子中,它有這個值,但是如果它不存在,我會添加這個** - Dcustom.properties =/fs0/share/custom.properties **值。 – Naga

+0

您可以查看用於命令行XML處理的xmlstarlet。 –

回答

0

在XML文件中,第一applicationServerInstance標籤與-Dcustom.properties =/FS0 /股/ custom.properties值jvmParameters。但在第二個applicationServerInstance標記中,jvmParameter的-Dcustom.properties =/fs0/clarity1/share/custom.properties值而不是-Dcustom.properties =/fs0/share/custom.properties。在這種情況下,我的代碼片段再添加一個Dcustom.properties值。

所以第二個applicationServerInstance標籤就是這樣的。

<applicationServerInstance id="app" serviceName=" App Server" rmiPort="15001" jvmParameters="-Xmx3072m -Xms512m -XX:-UseGCOverheadLimit -XX:MaxPermSize=196m -Dsun.lang.ClassLoader.allowArraySyntax=true -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/fs0/clarity1/clarity/logs -Xloggc:/fs0/clarity1/clarity/logs/app_gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Dcustom.properties=/fs0/clarity1/share/custom.properties -Dcustom.properties=/fs0/share/custom.properties" maxThreads="1000" programParameters="" distributed="false" runJobScheduler="false" useSSO="true" maxConcurrentJobs="10" runProcessEngine="false" messageTimeToLive="120" messageReceiverInterval="5" exceptionRunInterval="normal" maxXmlNodesLimit="150000"/> 

bash腳本:

#!/usr/bin/env bash 

file_name="/home/selvam/Scripts/test.xml" 
for i in $(grep -nE "<applicationServerInstance.*jvmParameters" $file_name | cut -d ':' -f1) 
do 
    jvm_parameters=$(head -$i $file_name | tail -1 | awk -F '"' '{ for (c=1; c<=NF; c++) if ($c ~ /jvmParameters/) print $(c+1)}') 
    echo $jvm_parameters | grep -q 'Dcustom.properties=/fs0/share/custom.properties' 
    if ([ $? -ne 0 ]) then 
     jvm_new_parameters=$(echo $jvm_parameters -Dcustom.properties=/fs0/share/custom.properties) 
     sed -i "${i}s%jvmParameters=\"$jvm_parameters\"%jvmParameters=\"$jvm_new_parameters\"%" $file_name 
    fi 
done 
0

AWK

awk -F"jvmParameters=" '{if((/applicationServerInstance/)&&($2!~/Dcustom.properties/)){sub("PrintHeapAtGC","PrintHeapAtGC -Dcustom.properties=/fs0/share/custom.properties")}print}' $inputfile 

將是另一個解決方案。將$ inputfile替換爲您要處理的文件的名稱。