Validate date and time in filename by awk

hi
i want to validate the date and time in filename

filename : mohan.moh.ccyymmdd.ccyymmdd.hhmmss.txt
 mohan_moh.20151222.20151222.122442.txt

i want code that check that date given in filename 20151222 in this format ccyymmdd else it mark file is not valid used in my OS detail is AIX 6 ksh

Does it have to be awk ? Pure shell:

IFS="." read X X D1 D2 T E < $(ls mo*.txt)
[ 8 -eq ${#D1} ] && [ 12 -ge ${D1:4:2} ] && [ 31 -ge ${D1:6:2} ]  && [ $D1 -gt 0 ] && echo OK || echo NOK
1 Like

With GNU utils everything is simpler but well... AIX 6

echo '20170001
20319900
20110232
20000001
20160230
20160229' |  while read d; do
  date -d$d '+%Y%m%d'; done
date: invalid date �20170001�
date: invalid date �20319900�
date: invalid date �20110232�
date: invalid date �20000001�
date: invalid date �20160230�
20160229

But I think this egrep expression is enough for practical needs (only for 2000-2016 years)

egrep '20[01][0-6](0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])'
1 Like

Thanks in advance but it not working

Please confirm what shell you are using

echo $SHELL
or set command

I want to use awk like this

awk -F '.' '{
IF($1== mohan_moh)
 then print ok 
elif ($2= --some regular expression to validate date) here $2 is 20151222
then print ok 
elif ($3= --some regular expression to validate date) here $3 is 20151222
then print ok
elif ($4= --some regular expression to validate time) here $4 is 122442
then print ok
else
 print nok
fi}

please suggest me something like so validate name string date and time in single awk command in AIX 6

---------- Post updated at 11:52 AM ---------- Previous update was at 11:50 AM ----------

So the assumption is $2 will be 20151222 or like YYYMMDD year month and day is this right ?

yes you right

I want to use system date. Can you check if date +"%Y%m%d" works

My idea is to use

awk -v date="$(date +"%Y%m%d" ) 

So I get a variable made which can be compared

date +"%Y%m%d" AND awk -v date="$(date +"%Y%m%d" ) 

NOT WORKING ALREADY TRY ON AIX 6
THANKS MAN

---------- Post updated at 12:07 PM ---------- Previous update was at 12:03 PM ----------

my sample data

filename : mohan.moh.ccyymmdd.ccyymmdd.hhmmss.txt
 mohan_moh.20151222.20151222.122442.txt

please suggest me something like so validate name string date and time in single awk command in AIX 6
my sample code

awk -F '.' '{
IF($1== mohan_moh)
 then print ok 
elif ($2= --some regular expression to validate date) here $2 is 20151222
then print ok 
elif ($3= --some regular expression to validate date) here $3 is 20151222
then print ok
elif ($4= --some regular expression to validate time) here $4 is 122442
then print ok
else
 print nok
fi}

Any of these work ?

The UNIX School: gawk - Date and time calculation functions

Can we use Perl instead ?

I have handled similar situations where nothing works no GNU date or gnu awk :frowning:

Yes you can use anything just i need a solution that work

Can you check perl version. I am assuming no perl modules are installed

---------- Post updated at 12:41 PM ---------- Previous update was at 12:27 PM ----------

Check this perl code. Works on my servers FreeBSD and RHEL

echo " mohan_moh.20160115.20160115.122442.txt" | perl dtchk.pl

FILENAME: dtchk.pl

use strict;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $yyyymmdd = sprintf "%.4d%.2d%.2d", $year+1900, $mon+1, $mday;

### CHECK THIS LINE
my $last="122442"; # Just make this to whatever required like Date time etc etc


# TEST with below file name to match my system date
#mohan_moh.20160115.20160115.122442.txt"

while (<STDIN>) {
        chomp;
        if ( $_ =~ m/mohan_moh\.\Q$yyyymmdd\E\.\Q$yyyymmdd\E\.\Q$last\E\.txt/ )
        { print "\n$_ FILEName is [ OK ]\n"; }
        else
        { print "\n $_ FILEName is [  BAD ]\n"; }
}

Can be improved a lot.