Hello All,
I have a requirement where i need to get the EXTRACT_DATE from a file and check if the date is of valid format or not and then mail it if it is not valid. Appreciate if you can help me with this.
Now i want to connect to Oracle sqlplus and pass this "2012-12-31" as an input parameter to following sql and write into log file and then grep for error message in log file if the to_date function errors out because of bad date format/data and then email out the error message.
select to_date('2012-12-31','yyyy-mm-dd') from dual
Appreciate your help and if there is another better way of handling this in shell scripting itself instead of connecting to Oracle DB to perform validation.
Yoda,
Thanks for your input it returns the expected output, when i execute this it took more time than my statement. Would it be possible to explain what you are doing in each step. appreciate if you can enlighten me up a bit.
awk '
{
# Match pattern EXTRACT_DATE= followed by zero or more occurrence of any character other than space [^ ]*
match ( $0, /EXTRACT_DATE=[^ ]*/ );
# match function sets the built-in variable RSTART to the index and RLENTGH to the length.
# assign variable DT = matched pattern using substr function and variables: RSTART, RLENTGH
DT = sprintf ( "%s", substr ( $0, RSTART, RLENGTH ) )
# remove EXTRACT_DATE=" and " from DT variable value
gsub ( /.*="|"$/, X, DT )
# assign value to an associative array: A for removing duplicate entries
A[DT]
}
END { # END Block
# for each key/index in associative array: A
for ( k in A )
# print key/index
print k
}
' file
I want to capture the UNINUM & EXTRACT_DATE in the following pattern so that i can go in a loop and assign them to variables to put much information in log files.
awk '
{
for ( i = 1; i <= NF; i++ )
{
if ( $i ~ /^UNINUM/ )
{
u = $i
gsub ( /.*="|"$/, X, u )
}
if ( $i ~ /^EXTRACT_DATE/ )
{
e = $i
gsub ( /.*="|"$/, X, e )
print u, e
}
}
}
' file
That was really quick. Many thanks
One last question though i should have asked it earlier.
"i" does it represent the line or line counter, bit confused as you were again using to assign the value to variables u,e
i represents each field in your record separated by blank space (by default).
NF represents the number of fields or the last field, using for loop you are going through each field (incrementing i) until last field (NF) is reached.