sed XML解析选择带有特殊字符的标签之间的文本,同时删除其他块

sed XML解析选择带有特殊字符的标签之间的文本,同时删除其他块

<JOB>我正在尝试解析由和之间的块组成的 XML ,我在 do while 循环中解析,在读取和</JOB>之间的块时,我还将块重定向到文件,同时删除它并退出,我尝试使用以下内容命令,但它似乎也在选择同一模式中的所有其他块,我对每个特殊字符使用转义字符,但没有得到所需的输出,您能否指出我出错的地方:<JOB></JOB>

sed '/\<JOB/,/\<\/JOB/!d;/\<\/JOB/q' /tmp/ftp_final >> /tmp/result_1

文本样本:

<JOB 
     ACTIVE_TILL="20141124" 
     APPLICATION="CTDS0320" 
     APPL_FORM="AFT" 
     APPL_TYPE="FILE_TRANS" 
     APPL_VER="6.1.01" 
     APR="1" 
     AUG="1" 
     AUTHOR="wyy" 
     AUTOARCH="0" 
     CHANGE_DATE="20190403" 
     CHANGE_TIME="141554" 
     CHANGE_USERID="ecsadmin" 
     CM_VER="610" 
     CONFIRM="0" 
     CREATION_DATE="20190328" 
     CREATION_TIME="105555" 
     CREATION_USER="singhm7" 
     CRITICAL="0" 
     CYCLIC="0" 
     CYCLIC_TOLERANCE="0" 
     CYCLIC_TYPE="Interval" 
     DAYS_AND_OR="OR" 
     DEC="1" 
     DESCRIPTION="Processes Source data files FTP from Source System" 
     DOCLIB="/prod/appl/ctds/Doc" 
     DOCMEM="energent_billing_delta.doc" 
     FEB="1" 
     GROUP="@CTDS-BW" 
     IND_CYCLIC="START" 
     INTERVAL="00000M" 
     JAN="1" 
     JOBNAME="energent_billing_delta-FTP" 
     JUL="1" 
     JUN="1" 
     MAR="1" 
     MAXDAYS="0" 
     MAXRERUN="0" 
     MAXRUNS="0" 
     MAXWAIT="7" 
     MAY="1" 
     MEMLIB="Not in use for application jobs" 
     MEMNAME="energent_billing_delta-FTP" 
     MULTY_AGENT="N" 
     NODEID="vprv0320" 
     NOV="1" 
     OCT="1" 
     OWNER="ctds_energent" 
     PARENT_TABLE="@CTDS" 
     PRIORITY="AA" 
     RETRO="0" 
     RULE_BASED_CALENDAR_RELATIONSHIP="OR" 
     SEP="1" 
     SHIFT="IGNOREJOB" 
     SHIFTNUM="+00" 
     SYSDB="0" 
     TASKTYPE="Job" 
     USE_INSTREAM_JCL="N" 
     WEEKSCAL="BCD-Y">
      <INCOND AND_OR="AND" NAME="CTDS0320-ENERGENT-BILLING-DELTA-OK" ODATE="ODAT"/>
      <OUTCOND NAME="CTDS0320-ENERGENT-BILLING-DELTA-FTP" ODATE="ODAT" SIGN="ADD"/>
      <AUTOEDIT2 NAME="%%FTP-ACCOUNT" VALUE="ctds_energent"/>
      <AUTOEDIT2 NAME="%%FTP-LOSTYPE" VALUE="Unix"/>
      <AUTOEDIT2 NAME="%%FTP-LUSER" VALUE="ctds"/>
      <AUTOEDIT2 NAME="%%FTP-ROSTYPE" VALUE="Windows"/>
      <AUTOEDIT2 NAME="%%FTP-RUSER" VALUE="[email protected]"/>
      <AUTOEDIT2 NAME="%%FTP-LPATH1" VALUE="/prod/appl/ctds/Publish/ENERGENT/*"/>
      <AUTOEDIT2 NAME="%%FTP-RPATH1" VALUE="/\Business Intelligence-Gas Consumption\Energent\Outgoing\Gas Consumption\"/>
      <AUTOEDIT2 NAME="%%FTP-PATH" VALUE="Not in use for application jobs"/>
      <AUTOEDIT2 NAME="%%FTP-USE_DEF_NUMRETRIES" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-RPF" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-CLEAR_ALL" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-CONNTYPE2" VALUE="FTP"/>
      <AUTOEDIT2 NAME="%%FTP-CONNTYPE1" VALUE="LOCAL"/>
      <AUTOEDIT2 NAME="%%FTP-LHOST" VALUE="Local"/>
      <AUTOEDIT2 NAME="%%FTP-RHOST" VALUE="hdq-nt164"/>
      <AUTOEDIT2 NAME="%%FTP-LPASSIVE" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-RPASSIVE" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-UPLOAD1" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-UPLOAD2" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-UPLOAD3" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-UPLOAD4" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-UPLOAD5" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TRANSFER_NUM" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TYPE1" VALUE="A"/>
      <AUTOEDIT2 NAME="%%FTP-TYPE2" VALUE="I"/>
      <AUTOEDIT2 NAME="%%FTP-TYPE3" VALUE="I"/>
      <AUTOEDIT2 NAME="%%FTP-TYPE4" VALUE="I"/>
      <AUTOEDIT2 NAME="%%FTP-TYPE5" VALUE="I"/>
      <AUTOEDIT2 NAME="%%FTP-MINSIZE1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-MINSIZE2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-MINSIZE3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-MINSIZE4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-MINSIZE5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TIMELIMIT1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TIMELIMIT2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TIMELIMIT3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TIMELIMIT4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TIMELIMIT5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-UNIQUE1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-IF_EXIST1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-IF_EXIST2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-IF_EXIST3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-IF_EXIST4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-IF_EXIST5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-SRCOPT1" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-SRCOPT2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-SRCOPT3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-SRCOPT4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-SRCOPT5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-DSTOPT1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-DSTOPT2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-DSTOPT3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-DSTOPT4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-DSTOPT5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-ABSTIME1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-ABSTIME2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-ABSTIME3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-ABSTIME4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-ABSTIME5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TRIM1" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TRIM2" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TRIM3" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TRIM4" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TRIM5" VALUE="1"/>
    </JOB>

示例代码(不完整):

#!/bin/sh
set -xv
rm /tmp/result* /tmp/ftp_final
sed '/^$/d' /tmp/FTPJobs_table007.xml >> /tmp/ftp_final
lines=`cat /tmp/ftp_final | wc -l`
while [ $lines -gt 1 ]
do   
#typeset -i temp=`sed -n '5,5p' /tmp/jobs_xa_final | grep DESC | wc -l`
#temp2=`sed -n '5,5p' /tmp/jobs_xa_final | grep DESC`
#echo $temp
#echo $temp2
#echo "nodesc"
sed '/\<JOB/,/\<\/JOB/!d;/\<\/JOB/q' /tmp/ftp_final >> /tmp/result_1
JOBNAME=`cat /tmp/result_1 | grep "JOBNAME=" | cut -d"=" -f2 | cut -d"\"" -f2`
WORKSTATIONHASH="WA_SAGT#"
echo $WORKSTATIONHASH$JOBNAME >> /tmp/result_final
cat /tmp/header.txt >> /tmp/result_final
FTLSERVEROPENTAG="<jsdlfiletransfer:server>"
FTRSERVER=`cat /tmp/result_1 | grep "FTP-RHOST" | cut -d "=" -f3 | cut -d "\"" -f2`
FTLSERVERCLOSINGTAG="</jsdlfiletransfer:server>"

预期输出:

WA_SAGT#MKT_AMER_FTP
 TASK
    <?xml version="1.0" encoding="UTF-8"?>
<jsdl:jobDefinition xmlns:jsdl="http://www.ibm.com/xmlns/prod/scheduling/1.0/jsdl" xmlns:jsdlfiletransfer="http://www.ibm.com/xmlns/prod/scheduling/1.0/jsdlfiletransfer" name="filetransfer">
  <jsdl:application name="filetransfer">
    <jsdlfiletransfer:filetransfer>
            <jsdlfiletransfer:downloadInfo>
                <jsdlfiletransfer:server>ws94mdm0</jsdlfiletransfer:server>
                <jsdlfiletransfer:localfile>/tmp/leonjob2.txt</jsdlfiletransfer:localfile>
                <jsdlfiletransfer:remotefile>/tmp/leonjob.txt</jsdlfiletransfer:remotefile>
                <jsdlfiletransfer:localCredentials>
                    <jsdl:userName>wauser</jsdl:userName>
                    <jsdl:password>{aes}Gt+aN5nGz5dUBNaS/TJHoab0qwDZzII4gQPBi8AhPz0=</jsdl:password>
                </jsdlfiletransfer:localCredentials>
                <jsdlfiletransfer:remoteCredentials>
                    <jsdl:userName>wauser</jsdl:userName>
                    <jsdl:password>{aes}G7Rhyk5NkonXiV3AVSQ8pjVXElHgU0tE1RUveivv0xA=</jsdl:password>
                </jsdlfiletransfer:remoteCredentials>
                <jsdlfiletransfer:protocol>SSH</jsdlfiletransfer:protocol>
                <jsdlfiletransfer:transferMode>binary</jsdlfiletransfer:transferMode>
            </jsdlfiletransfer:downloadInfo>
        </jsdlfiletransfer:filetransfer>
  </jsdl:application>
</jsdl:jobDefinition>
 DESCRIPTION "Added by composer."
 RECOVERY STOP

问候,斯里拉姆.V

答案1

xml使用sed, grepor进行解析awk是复杂且容易出错的。您应该使用专门的 XML 解析器,例如xmlstarlet.

不确定您的输出到底应该是什么样子,但从您的脚本中我认为它类似于以下内容。这将输出JOBNAME FTP_RHOST_VALUE每个的列表JOB

xmlstarlet sel -t \
-m '//JOB' \
-v './@JOBNAME' \
-o $'\t' \
-v './AUTOEDIT2[@NAME="%%FTP-RHOST"]/@VALUE' \
-n \
/tmp/FTPJobs_table007.xml

示例文本的输出:

energent_billing_delta-FTP  hdq-nt164

你可能需要安装xmlstarlet,例如:

sudo apt install xmlstarlet

相关内容