Check Date Format And Email Out


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check Date Format And Email Out
# 1  
Old 09-17-2013
Check Date Format And Email Out

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.

I did the following so far.

Code:
awk '{for(i=1;i++<=NF;)if($i~/^EXTRACT_DATE/) print $i}' FCBTEXAS_v1.xml

got the following output
Code:
EXTRACT_DATE="2012-12-31"

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.

Code:
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.

Thanks much.

Last edited by vbe; 09-17-2013 at 01:19 PM.. Reason: code tags not icode...
# 2  
Old 09-17-2013
something like the following would work I guess:
Code:
'awk '{for(i=1;i++<=NF;)if($i~/^EXTRACT_DATE/) print $i}' FCBTEXAS_v1.xml`
sqlplus user/pw@sid << EOC | grep ORA && mail_script 
select to_date('2012-12-31','yyyy-mm-dd') from dual ;
EOC

POSIX::mktime does not do what the perldoc says it should do on my Perl.... please ignore previous version

Last edited by Skrynesaver; 09-17-2013 at 02:44 PM.. Reason: Remembered to load POSIX module
# 3  
Old 09-17-2013
When i do this
Code:
awk '{for(i=1;i++<=NF;)if($i~/^EXTRACT_DATE/) print $i}' AgriBank_v2.xml

I got below as it has multiple EXTRACT_DATE entries in the file which is valid.

Output:
Code:
EXTRACT_DATE="2012-09-30"
EXTRACT_DATE="2012-09-30"
EXTRACT_DATE="2012-09-30"
EXTRACT_DATE="2012-09-30"

Now i want to get only the
1) Date Portion
2) Unique Date
3) Remove double quotes.

So i came up with the below is there a better way to achieve this.

Code:
awk '{for(i=1;i++<=NF;)if($i~/^EXTRACT_DATE/) print $i}' AgriBank_v2.xml | awk -F"\"" '{print $2}' | sort -u

Output:
Code:
2012-09-30

Also i am still working on the validating the date portion as all scripts i found online seems to pretty complex. appreciate your inputs.

Thank you.
# 4  
Old 09-17-2013
Another awk approach:
Code:
awk '
        {
                match ( $0, /EXTRACT_DATE=[^ ]*/ );
                DT = sprintf ( "%s", substr ( $0, RSTART, RLENGTH ) )
                gsub ( /.*="|"$/, X, DT )
                A[DT]
        }
        END {
                for ( k in A )
                        print k
        }
' file

# 5  
Old 09-17-2013
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.

Thank you.
# 6  
Old 09-17-2013
Here is a brief explanation:
Code:
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 hope this helps.
# 7  
Old 09-17-2013
That really helps. However i started coding using my original statement but struck can you help.

Code:
awk '{for(i=1;i++<=NF;)if($i~/^EXTRACT_DATE|^UNINUM/) print $i}' AgriBank_v2.xml

Output:
Code:
UNINUM="0722075"
EXTRACT_DATE="2012-09-30"
UNINUM="0722146"
EXTRACT_DATE="2012-09-30"
UNINUM="0722502"
EXTRACT_DATE="2012-09-30"
UNINUM="0722643"
EXTRACT_DATE="2012-09-30"

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.

Code:
0722075 2012-09-30
0722146 2012-09-30
0722502 2012-09-30
0722643 2012-09-30

please help.

Thank you.

Last edited by Ariean; 09-17-2013 at 06:06 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command to check date format DDMMYYYY

Record: Record1|Record2|Record3|Record4|Record5|DATE1|DATE2 Need to Check DATE1 & DATE2 is in DDMMYYYY format in a file. records which not meet the date format DDMMYYYY extract to other file. (1 Reply)
Discussion started by: vivekn
1 Replies

2. Shell Programming and Scripting

How to check if date format is correct?

Hi! how do i know if the input is the same as the required date format? the date should be dd/mm/YYYY ex. 2/3/2012 or 15/11/2012 all the following conditions must return an error: *input of string *day is > 31 or < 1 *month is > 12 or < 1 *year is < 2013 suppose the date format is stored... (1 Reply)
Discussion started by: angilulu
1 Replies

3. Shell Programming and Scripting

finding date numeral from file and check the validity of date format

hi there I have file names in different format as below triss_20111117_fxcb.csv triss_fxcb_20111117.csv xpnl_hypo_reu_miplvdone_11172011.csv xpnl_hypo_reu_miplvdone_11-17-2011.csv xpnl_hypo_reu_miplvdone_20111117.csv xpnl_hypo_reu_miplvdone_20111117xfb.csv... (10 Replies)
Discussion started by: manas_ranjan
10 Replies

4. Shell Programming and Scripting

PERL : hhmiss - Date format check and replace

I have a filename, This can be any of any format, I want to check if the filename has hours,mins and seconds part. If it is present, i want to replace it with a " * " (star symbol) output needed: IMP: The time part can be in any pattern. How can this be done?:confused:... (3 Replies)
Discussion started by: irudayaraj
3 Replies

5. Shell Programming and Scripting

Date format check and replace string in PERL

I have got few date format patterns like "yyyymmdd", "yy_mm_dd" etc. There can be any combination of such patterns. I have used add_delta_days to find "yyyy", "yy", "mm", "dd" for the current date and saved them to different variables like "$y1", "$y2", "$m1" etc In one line, i want to... (10 Replies)
Discussion started by: irudayaraj
10 Replies

6. Shell Programming and Scripting

Need to check date format yyyymm

My source file having one date column. The formate of the date column is yyyymm. I need to validate whether all the rows are in same format in the given file. If it is not I have captured that records in a separate file. I am very new to Unix. I don't how to achieve this. Plz help me to achieve... (2 Replies)
Discussion started by: suresh01_apk
2 Replies

7. Shell Programming and Scripting

Check input date format?

how to check input date format. for example $input_date must be in format dd.mm.gg script is execute like this: bin/script1.sh 14.12.2009 script1.sh code: #!/bin/sh input_date=$1 CMD="/app/si/test/test.sh $input_date" echo "*****" $CMD (2 Replies)
Discussion started by: waso
2 Replies

8. UNIX for Dummies Questions & Answers

Any simple method to check date format (YYDDD)?

Hi Guru's, Are there any simple method to check the date format which is in "YYDDD" (ex: 08002 for 02-Jan-2008)? Eventhough this can be implemented in several way's, I need the simpler one. Any idea would be much appreciated. Thanks in advance, Mysore Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies

9. Shell Programming and Scripting

Check the format of Date

Hi, I have a date field in my input file. I just want to check if its in the format "DD-MM-YYYY". Is there any command which can achieve this? Thanks and Regards, Abhishek (2 Replies)
Discussion started by: AAA
2 Replies

10. Shell Programming and Scripting

Check for the correct date format in UNIx

Hi All, I am getting two input from User for Date from the command prompt when my script is executed . The date format i am taking is : DD-MM-YYYY so is there any method in Unix to validate the two input date. There might be many cases for these two date to be invalid.... (1 Reply)
Discussion started by: rawatds
1 Replies
Login or Register to Ask a Question

Featured Tech Videos