Date column validation in files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Date column validation in files
# 1  
Old 11-18-2014
Date column validation in files

HI,

i have to do some file validations on which i am receving daily.could any one please let me know how we can able to do validations on below scenarios

1) On Date column Ex
HTML Code:
 If i am receving date in format of YYYYMMDD---- 20141118  ---  here i have to check the date value 20141142 if the value is not correct i have to raise issue
# 2  
Old 11-18-2014
bhaskar v,

Can you provide some sample data, and what you have tried thus far?

- D
# 3  
Old 11-18-2014
Sample data pasted below
Code:
20110227
           20120528
           20141142
            20110230


in the above sample data 3 & 4 dates are not valid.so i want highlight those two dates to my client

Last edited by vbe; 11-18-2014 at 09:17 AM.. Reason: replaced HTML by code tags...
# 4  
Old 11-18-2014
YMMV...
Code:
use strict;
use warnings;
use Time::Local;

sub ckdate {
   my ($year, $mon, $mday) = @_;
   return join '', @_ if defined eval { timegm(0, 0, 0, $mday, $mon-1, $year); };
   return join '', '<span class="highlight">', @_, '</span>';
}

while (<>) {
   s{(\d{4})(\d\d)(\d\d)}{ckdate($1, $2, $3)}e;
   print;
}

---------- Post updated at 05:57 AM ---------- Previous update was at 05:56 AM ----------

generates:
Code:
20110227
           20120528
           <span class="highlight">20141142</span>
            <span class="highlight">20110230</span>

# 5  
Old 11-18-2014
Hi

i am so sorry i want understand the above mentioned logic could you place comments over there

actually i have pasted for one column
my scenario is if i am having more one column in a file
like below
Code:
col1, col2,col3,date
1,2,3,20110825
2,3,4,20110229
6,8,9,20141142

in the above last row date is invalid how can i find that data is invalid

Last edited by vbe; 11-18-2014 at 09:17 AM.. Reason: Do you mind using code tags? not HTMl or icode...
# 6  
Old 11-19-2014
Your sample date changes things a bit:
Code:
use strict;
use warnings;
use Time::Local;

sub ckdate {
   my $date = shift @_;
   my ($year, $mon, $mday) = $date =~ m{^(\d\d\d\d)(\d\d)(\d\d)$};
   return unless defined $mday;
   return defined eval { timegm(0, 0, 0, $mday, $mon-1, $year); };
}

$, = ' ';
$\ = "\n";

while (<>) {
   chomp;
   my @F = split ',';
   next if ckdate pop @F;
   print '?', $_;
}

So what is happening:
Code:
sub ckdate {

A function that will return true if the first argument is a valid date.
Code:
   my $date = shift @_;
   my ($year, $mon, $mday) = $date =~ m{^(\d\d\d\d)(\d\d)(\d\d)$};

Split the date (first argument) into its year, month, and day of the month
Code:
   return unless defined $mday;

Return undef if the date was not of the form YYYYMMDD
Code:
   return defined eval { timegm(0, 0, 0, $mday, $mon-1, $year); };

timegm converts its arguments into a number of seconds of the epoch, of undef if the arguments are invalid. The 'eval' suppresses warning messages.
Code:
   my @F = split ',';

Split the line into comma separated fields
Code:
   next if ckdate pop @F;

Skip to the end of the loop if ckdate returns true: the last field was a valid date.
Code:
    print '?', $_;

Print something to indicate that the date is invalid.

Additional checks could be made to further validate the date field, depending on your requirements. For instance, if this is a table of expirations, then dates in the past would be invalid.

Hope this helps.

Of course, there are many other way to validate dates, I just used a library function.
# 7  
Old 11-19-2014
Thanks very much derekludwig

i am fine with the above mentioned logic but my doubt is will it work for if we are having many columns Smilie


Code:
col1, col2,col3,date
1,2,3,20110825
2,3,4,20110229
6,8,9,20141142

in the above last row date is invalid how can i find that data is invalid

Last edited by Franklin52; 11-19-2014 at 12:18 PM.. Reason: Please use regular code tags for multiple lines
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Date validation

Hi folks, I new to shell script . I want to know how to validate a String as valid date example: 20150712 ---> valid date 20160524-->valid 201605T12-->invalid date 12341234--->invalid date we need to valid string( yyyymmdd) to date in SunOS 5.10 please give some idea to validate... (9 Replies)
Discussion started by: srinadhreddy27
9 Replies

2. Shell Programming and Scripting

Validation of date from file name

I'm writing a shell script for cleanup of older files from various sub-directories inside a main directory The structure of directories is as below: Logs daily online archive weekly online archive... (1 Reply)
Discussion started by: asyed
1 Replies

3. Shell Programming and Scripting

Date validation

File contains below data,how to validate the date using awk command or any command. date formate is fixed as "YYYYMMDD" test1|20120405 test2|20121405 output should be: test1|20120405 Thanks (2 Replies)
Discussion started by: bmk
2 Replies

4. UNIX for Dummies Questions & Answers

Unix date validation

Dears, I am working on a batch that processes file with name containing date prefix eg., 20101222_file.dat. The logic is to process files in order. Eg., 20101225 must be processed only after 20101222. Ok first glance it looked simple, it use a variable to check this date value as number and... (2 Replies)
Discussion started by: naraink
2 Replies

5. Shell Programming and Scripting

Date validation

Hi, I have a script which runs on specific sunday. If that script runs on the sunday i want to execute another script on following wednesday. I have a log for that server. My wednesday scripts needs to check the sunday run log timestamp and if it matches it should run. Please help. Thanks,... (1 Reply)
Discussion started by: Krrishv
1 Replies

6. Shell Programming and Scripting

Date Validation

the user have to input the date format in mmddmmhhyyyy (month,date,minutes,hour,year) i want a shell script to check whether the user has properly input in the above said manner. kindly advice (2 Replies)
Discussion started by: vkca
2 Replies

7. UNIX for Dummies Questions & Answers

Sorting list of files per date column

Hi all, I have a pecular issue in sorting these files (not an ls -lrt) in Solaris environment. All the below files are modified on November 4th, but I want to sort these files as per date (eg: 01May07_1623 = ddmmmyy_hhmm) Nov 4 18:27 SONYELEC00.GI22973.01May07_1623.gpg Nov 4 18:27... (10 Replies)
Discussion started by: shivaastrogun
10 Replies

8. Shell Programming and Scripting

Date Validation:

Hi I have below file with 3 rd column as date ....i want to make 3 column to mm/dd/yyyy . in the below file 2 row date is like 1/23/1994 so i want to append '0' to month i,e. 01/23/1994 and in the 3 row date is like 6/4/1994 ---so i want this to 06/04/1994 Source:... (1 Reply)
Discussion started by: satyam_sat
1 Replies

9. Shell Programming and Scripting

Date Validation again

I want a sample Date validation script using if loops. The script should first compare "year".If the year is lesser than the current year,It should go for "month" checking. I have the script that splits the date into year,month and date.I want only the checking part. My if loop checking is not... (4 Replies)
Discussion started by: dave_nithis
4 Replies

10. Shell Programming and Scripting

string validation on a column

Hi there I have a file that I recieve that looks like the folowing, As these details are being manually entered by the customer, we sometimes get a few typos on 4th column (the 14 digit number starting with 42....) As you can see the first one has a space as the first character and then a 13 number... (3 Replies)
Discussion started by: hcclnoodles
3 Replies
Login or Register to Ask a Question