<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
감사합니다, Sriram.V
답변1
xml
sed
, or 를 사용한 grep
구문 분석은 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