Hi
I am new to scripting and I have a requirement to grep a value from large numbers of xml files and see if this value exist then I want to check the date and see if it falls between two dates (eg: today and feb 17), then print the name of file. the values in xml file is as follow
Name="TNT_License_End" Value="2012-12-29"
first check for TNT_Licencse_End then for the date if it actually between today and Feb 17.
Thank you in anticipation for you help.
grep does not understand dates, it is not a programming language. awk is a programming language, and better suited.
You are lucky, the dates are in YYYY-MM-DD order, which is directly sortable and comparable with > < string comparisons.
We probably need to see more of the XML.
This might work, depending on your XML.
$ awk -v FIRST="2012-12-28" -v LAST="2012-12-31" '
(NR==1) { F=FILENAME }
F!=FILENAME { if(M) print F; F=FILENAME ; M=0 }
/TNT_License_End/ && match($0, "Value=\"[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\"") {
V=substr($0,RSTART+7,RLENGTH-8)
if((V <= LAST)&&(V >= FIRST)) M=1 }
END { if(M) print F }' input1.xml input2.xml
input1.xml
$
#!/bin/bash
CURR_DATE=$( date +"%s" )
PREV_DATE=$( date -d"2012-02-17" +"%s" )
extr_xmld=$( awk -F"Value=" '/TNT_License_End/ { gsub("\"","",$2); print $2; } ' xml_file )
XMLF_DATE=$( date -d"$extr_xmld" +"%s" )
if [ ${XMLF_DATE} -ge ${PREV_DATE} ] && [ ${XMLF_DATE} -le ${CURR_DATE} ]
then
echo "Is between Feb 17 & Today"
else
echo "Is not between Feb 17 & Today"
fi
Like I said, YYYY-MM-DD dates are directly sortable as strings. There's no need to convert them to epoch seconds.
thanks alot for your prompt response, I think the code is very close to what I expect but the date I mean is going to end on feb 17th of 2013, also there is an error -ge: unary operator expected.
thank you again for your help
Looks like XMLF_DATE is not set, perform below changes:-
#!/bin/bash
CURR_DATE=$( date +"%s" )
PREV_DATE=$( date -d"2012-02-17" +"%s" )
extr_xmld=$( awk -F"Value=" '/TNT_License_End/ { gsub("\"","",$2); print $2; } ' xml_file )
if [ ! -z $extr_xmld ]
then
XMLF_DATE=$( date -d"$extr_xmld" +"%s" )
if [ ${XMLF_DATE} -ge ${PREV_DATE} ] && [ ${XMLF_DATE} -le ${CURR_DATE} ]
then
echo "Is between Feb 17 & Today"
else
echo "Is not between Feb 17 & Today"
fi
fi
I suggest my solution, which works in one command for many files instead of four commands for one file. It also doesn't require the nonstandard -d GNU date extension -- AIX almost certainly doesn't have it.