Visit Our UNIX and Linux User Community


Validate date and time in filename by awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Validate date and time in filename by awk
# 1  
Old 01-15-2016
Validate date and time in filename by awk

hi
i want to validate the date and time in filename
Code:
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

Last edited by MOHANP12; 01-15-2016 at 10:24 AM..
# 2  
Old 01-15-2016
Does it have to be awk? Pure shell:
Code:
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


Last edited by RudiC; 01-15-2016 at 08:22 AM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 01-15-2016
With GNU utils everything is simpler but well... AIX 6

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

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


Last edited by yazu; 01-15-2016 at 11:02 AM.. Reason: bug in egrep expression
This User Gave Thanks to yazu For This Post:
# 4  
Old 01-15-2016
Quote:
Originally Posted by RudiC
Does it have to be awk? Pure shell:
Code:
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


Thanks in advance but it not working
# 5  
Old 01-15-2016
Quote:
Originally Posted by MOHANP12
Thanks in advance but it not working

Please confirm what shell you are using


echo $SHELL
or set command
# 6  
Old 01-15-2016
I want to use awk like this
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}

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 ----------

Quote:
Originally Posted by chakrapani
Please confirm what shell you are using


echo $SHELL
or set command

Quote:
my shell script is running on AIX 6 ksh
# 7  
Old 01-15-2016
So the assumption is $2 will be 20151222 or like YYYMMDD year month and day is this right ?

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract date and time part from filename

Hi, I am facing one scenario in which I need to extract exact position of date and time from the name of the files. For example, Below is the record in which I need to extract position of YYYYMMDD,HHMISS and YYMMDD. Date and time variables can come more than once. I need to use these position... (13 Replies)
Discussion started by: Prathmesh
13 Replies

2. UNIX for Beginners Questions & Answers

How to extract date and time from filename?

Hi, I'm totally new in sell script and working with a shell code. I want to extract the date and time from the filenames. The filenames are different but all of them begins with WI_ SCOPE_: WI_SCOPE_DATA_CHANGE_2017-09-12_15-30-40.txt WI_SCOPE_BACK_COMPLETE_QUEUE_2017-09-12_15-31-40.txt... (5 Replies)
Discussion started by: Home
5 Replies

3. Shell Programming and Scripting

Validate date file format using awk

Here is my sample data Test.txt column 1|columne 2|start Date|end Date test|test|03/24/2014|03/24/2014 test|test|03/24-2014|03/24/2014 test|test|03/24/2014|03/24/2014 test|test|03/24/2014|03/24-2014 test|test|03/24/2014|03/24/2014 Now in the file i am expecting the date fields should be... (4 Replies)
Discussion started by: krish2014
4 Replies

4. UNIX for Dummies Questions & Answers

Converting string date time to unix time in AWK

I'd like to convert a date string in the form of sun aug 19 09:03:10 EDT 2012, to unixtime timestamp using awk. I tried This is how each line of the file looks like, different date and time in this format Sun Aug 19 08:33:45 EDT 2012, user1(108.6.217.236) all: test on the 17th ... (2 Replies)
Discussion started by: bkkid
2 Replies

5. Shell Programming and Scripting

append a filename with system date and time

Hi, There are similar kind of posts, but none seems like working for me. Please correct me if I'm wrong. I need append/rename file abc.txt with file processed date and time like abc_systemdatetimestamp.txt and move it to different folder. for example I have /source/data/abc.txt ... (1 Reply)
Discussion started by: amsn08
1 Replies

6. UNIX for Dummies Questions & Answers

Shell Scripts - Append a filename with date and time....

Hello, I need to create a shell script that appends a filename to create a name with the date and time appended that is guaranteed to not exist. That is, the script insures you will not overwrite a file with the same name. I am lost with this one. I know I need to use date but after that I am... (3 Replies)
Discussion started by: citizencro
3 Replies

7. UNIX for Dummies Questions & Answers

Adding Date & time stamps to filename

I need to edit the file name with date and time while writing the script. please help. (1 Reply)
Discussion started by: manish.s
1 Replies

8. UNIX for Advanced & Expert Users

find formatted filename with date time

Hi, I operate and use HF radars along the California coast for ocean surface currents. The devices use Mac OS as the control and logging software. The software generates thousands of files a week and while I've used PERL in the past to solve the problems of finding files I come to realize some... (6 Replies)
Discussion started by: dpath2o
6 Replies

9. UNIX for Dummies Questions & Answers

Insert date/time within a filename

Hi Guys, I need to script the renaming of files as followins: files: firstjd secondjo thirdjv My script needs to insert the date/time infront of the last 2 characters of the filenames above, any ideas greatly received :) the letters before the last 2 characters could change, I'm only... (7 Replies)
Discussion started by: cooperman
7 Replies

10. UNIX for Dummies Questions & Answers

Renaming files to have date/time in filename

I have a program that will export my data to a single file, but it assigns a file name that is overridden every time I run the program. I need to change the file name to have a sequential number in the filename. How do I rename a file so that the filename contains the system date and time. I want... (5 Replies)
Discussion started by: wayneb
5 Replies

Featured Tech Videos