2016-10-20 101 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替换为您要处理的文件的名称。