Multiple command execution inside awk command during xml parsing

below is the output xml string from some other command and i will be parsing it using awk

cat /tmp/alerts.xml
 
 <Alert id="10102" name="APP-DS-ds_ha-140018-componentFailure-S" alertDefinitionId="13982" resourceId="11427" ctime="1359453507621" fixed="false" reason="If Event/Log Level(ANY) and matching substring "failed on server"     Log: 10.19.123.197: 0:25:48.04, 1.3.6.1.4.1.1453.4.9.1.3.0.1, The service has failed on this server and caused an HA failover to occur., CLR OCCAS failed on server 10.19.123.197, failure, 2013-01-29,04:58:27.6,--5:0, CLEAR, 10.19.123.197, failure, 2013-01-29,04:58:27.6,--5:0, CLEAR, The service has"/>
.
.
.
etc ect

which i will be parsing using below command

awk -F'[=|"|<|>|,]' '{for(i=1;i<=NF;i++){
 if($i=="Alert id") {
  if(id!="")
        if(dt!=""){ printf "ID=%d\nNAME=%s\nFIXED=%s\nDATE=%s\nAlDefID=%d\n\n", id,nm,fx,dt,alDFid; }
  id=($i=="Alert id")?$(i+2):id; }
  nm=($i==" name")?$(i+2):nm;
  fx=($i==" fixed")?$(i+2):fx;
  dt=($i~/^ [0-9]+-/)?$i" "$(i+1):dt;
  alDFid=($i==" alertDefinitionId")?$(i+2):alDFid;
 }
}END{
 if(dt!=""){
 printf "ID=%d\nNAME=%s\nFIXED=%s\nDATE=%s\nAlDefID=%d\n\n", id,nm,fx,dt,alDFid;
}
}' /tmp/alerts.xml
 

output will be as below

ID=10102
NAME=APP-DS-ds_ha-140018-componentFailure-S
FIXED=false
DATE= 2013-01-29 04:58:27.6
AlDefID=13982
.
.
.etc etc many entries will be there

what i want to achieve is
>in the awk command i want to implement a check for date, first i will parse the date obtained from the query to only date and no time stamp and also system date

myDate=$( date --date="$DATE"  +%Y%m%d )
sysDate=$( date +%Y%m%d )

and if this date is older than system date then dont print from awk

if [[ $myDate -eq $sysDate ]] ;then 
   print
else
   skip all
fi

>also i need to perform one more operation

sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh  alertdefinition list --id=13982 --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true | grep priority | awk -F'priority=' '{print $2}' | cut -d'"' -f2

whose output will be

3

PS: i am not good at parsing xml so i did some my own awk and cut to get priority value from xml :slight_smile:

the above output is nothing but the priority obtained from that id and display it on console immediately.

--------------
so final output will be

ID=10102
NAME=APP-DS-ds_ha-140018-componentFailure-S
FIXED=false
DATE= 2013-01-29 04:58:27.6
AlDefID=13982
priority=3.
.
.etc etc many entries will be there
 

what i want to do is, use this hqapi.sh command inside mail xml parsing AWK command for each iteration and print priority then and there,
i tried using the hqapi command in quotes `` inside awk but it dint work.
any help on this will be deeply appreciated. thanks

PS:also if i succeed in all the above, is there any way i could use echo commands in awk so that i can put check in awk for printing priority in words say 1-lOW,2-MED,3HIGH in colour code using

echo -e "\e[1;31mHIGH\e[0m"

so the output

priority=HIGH

---------- Post updated at 09:37 PM ---------- Previous update was at 07:46 PM ----------

so far i implemented below stuff...

sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh  alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml
 
awk -F'[=|"|<|>|,]' '{for(i=1;i<=NF;i++){
 if($i=="Alert id") {
  if(id!="")
        if(dt!=""){
cmd="date  +%Y%m%d"
cmd | getline sysDate
print sysDate
close(cmd)
cmd2="date --date=dt  +%Y%m%d"
cmd2 | getline myDate
print myDate
close(cmd2)
cmd3="sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh  alertdefinition list --id=13671  --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true | grep priority | awk -F'priority=' '{print $2}' | cut -d'"' -f2 "
cmd3 | getline priority
print priority
close(cmd3)
 
 printf "ID=%d\nNAME=%s\nFIXED=%s\nDATE=%s\nAlDefID=%d\n\n", id,nm,fx,dt,alDFid; }
  id=($i=="Alert id")?$(i+2):id; }
  nm=($i==" name")?$(i+2):nm;
  fx=($i==" fixed")?$(i+2):fx;
  dt=($i~/^ [0-9]+-/)?$i" "$(i+1):dt;
  alDFid=($i==" alertDefinitionId")?$(i+2):alDFid;
 }
}END{
 if(dt!=""){
 printf "ID=%d\nNAME=%s\nFIXED=%s\nDATE=%s\nAlDefID=%d\n\n", id,nm,fx,dt,alDFid;
}
}' /tmp/alerts.xml

Additions are :

cmd="date  +%Y%m%d"
cmd | getline sysDate
print sysDate
close(cmd)
cmd2="date --date=dt  +%Y%m%d"
cmd2 | getline myDate
print myDate
close(cmd2)
cmd3="sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh  alertdefinition list --id=13671  --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true | grep priority | awk -F'priority=' '{print $2}' | cut -d'"' -f2 "
cmd3 | getline priority
print priority
close(cmd3)

stuck at the place where i need to parse date present in 'dt' variable.. its not considering dt even if i put $ behind it. its throwin error as

 
date: invalid date `dt'

and for running the script , the cmd3 is not able to process it, its showing as

awk: cmd. line:14: cmd3="sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh  alertdefinition list --id=13671  --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true | grep priority | awk -Fpriority= {print
awk: cmd. line:14:      ^ unterminated string

Can anyone help me to proceed further!!!!.. i see no one has replied to this.. the awk command looks complex but its actually simple...

JAVA, PERL and C++ have true XML parsers. Awk can handle some xml layouts but might get tangled in others just as valid. Tools like XPath, XQuery, XSLT and JAX can script XML solutions. XQuery - Wikipedia, the free encyclopedia Java XML - Wikipedia, the free encyclopedia

:frowning: dint help.
i just need the solution for running a script inside awk and storing the output in a variable and also using date variable inside awk for comparison. Is it possible?