awk question

Hello all,

I have ONE big xml file that contains hundreds of <JOB> ... </JOB> entries like the following:

  <JOB
   APPLICATION="APP"
   APR="0"
   AUG="0"
   AUTHOR="AUT"
   AUTOARCH="0"
   CMDLINE="/tmp/test1 %%var"
   CONFIRM="1"
   CREATION_DATE="20100430"
   CREATION_TIME="130739"
   CREATION_USER="user"
   CRITICAL="0"
   CYCLIC="0"
   CYCLIC_TYPE="Interval"
   DAYS_AND_OR="OR"
   DEC="0"
   DESCRIPTION="This is a test for"
   FEB="0"
   IND_CYCLIC="START"
   INTERVAL="00000M"
   JAN="0"
   JOBNAME="JOB1"
   JUL="0"
   JUN="0"
   MAR="0"
   MAXDAYS="0"
   MAXRERUN="0"
   MAXRUNS="0"
   MAXWAIT="20"
   MAY="0"
   MEMNAME="JOB1"
   MULTY_AGENT="N"
   NODEID="SERVER1"
   NOV="0"
   OCT="0"
   OWNER="root"
   RETRO="0"
   SEP="0"
   SHIFT="IGNOREJOB"
   SHIFTNUM="+00"
   SYSDB="0"
   TAG_RELATIONSHIP="OR"
   TASKTYPE="Command"
   USE_INSTREAM_JCL="N"
>
   <QUANTITATIVE NAME="DRIVE1" QUANT="1"/>
   <OUTCOND NAME="JOB1-OK" ODATE="ODAT" SIGN="ADD"/>
   <AUTOEDIT EXP="%%SYSTEM=server1"/>
  </JOB>
  <JOB>
  ....
  </JOB>

  <JOB>
  ....
  </JOB>

For each <JOB> ... </JOB> entrie I would like to update DESCRIPTION tag and add at the end of it the value of the variable %%SYSTEM like the following:

DESCRIPTION="This is a test for server1"

Cna someone help me please?

Thanks in advance

perl -i -0pe 's/(?<=DESCRIPTION=")([^"]+)(.*?)(?<=%%SYSTEM=)([^"]+)/\1 \3\2\3/sg' file.xml
1 Like

Hi bartus11,

Could you pleas explain the code ?

Worked perfectly, thanks a lot bartus11!! :wink:

---------- Post updated at 02:10 PM ---------- Previous update was at 01:39 PM ----------

One more thing bartus11 :o

Always in each <JOB> ... </JOB> tag:

can you also help me how to delete lines containing <QUANTITATIVE NAME="DRIVE1" QUANT="1"/> when JOBNAME starts by JOB

and also how to add the same line contaning <QUANTITATIVE NAME="DRIVE2" QUANT="1"/> before <OUTCOND NAME....> when JOBNAME starts by JOB1 ?

Many thanks!

Post sample input and desired output for those actions.

OK I'll try to be more specific.

Can you help me with one command that deletes lines containing <QUANTITATIVE NAME="DRIVE1" QUANT="1"/> in all JOB entries when value of JOBNAME starts by JOB1. Desired output would be:

   ...
  <JOB
   ...
   JOBNAME="JOB1"
   ...
>
   <OUTCOND NAME="JOB1-OK" ODATE="ODAT" SIGN="ADD"/>
   <AUTOEDIT EXP="%%SYSTEM=server1"/>
  </JOB>
  <JOB
   ...
   JOBNAME="JOB2"
   ...
>
   <QUANTITATIVE NAME="DRIVE2" QUANT="1"/>
   <OUTCOND NAME="JOB2-OK" ODATE="ODAT" SIGN="ADD"/>
   <AUTOEDIT EXP="%%SYSTEM=server1"/>
  </JOB>
   ...

Second command: Add the line <QUANTITATIVE NAME="DRIVE2" QUANT="2"/> before </JOB> in all JOB entries when JOBNAME starts by JOB2. Desired output would be:

   ...
  <JOB
   ...
   JOBNAME="JOB1"
   ...
>
   <QUANTITATIVE NAME="DRIVE1" QUANT="1"/>
   <OUTCOND NAME="JOB1-OK" ODATE="ODAT" SIGN="ADD"/>
   <AUTOEDIT EXP="%%SYSTEM=server1"/>
  </JOB>
  <JOB
   ...
   JOBNAME="JOB2"
   ...
>
   <QUANTITATIVE NAME="DRIVE1" QUANT="1"/>
   <OUTCOND NAME="JOB2-OK" ODATE="ODAT" SIGN="ADD"/>
   <AUTOEDIT EXP="%%SYSTEM=server1"/>
   <QUANTITATIVE NAME="DRIVE2" QUANT="2"/>
  </JOB>
   ...

Once again thanks for for your great help! :slight_smile:

 perl -i -0pe 's/(JOBNAME="JOB1".*?)\s+<QUANTITATIVE NAME="DRIVE1" QUANT="1"\/>/\1/sg;s/(JOBNAME="JOB2".*?)(\s+<\/JOB>)/\1\n   <QUANTITATIVE NAME="DRIVE2" QUANT="2"\/>\2/gs'
1 Like

Worked as I expected, thanks again for your great help bartus11! :wink:

local $/="</JOB>\n";
while(<DATA>){
 s/(.*DESCRIPTION="[^"]+)(".*?<AUTOEDIT EXP="%%SYSTEM=)([^"]+)/$1." ".$3.$2.$3/m
se;
 print;
}
__DATA__
your data here