Been thinking about this problem and thought I'd give it a bit of a go with gawk.
This currently only supports %d, %m, %Y and %a (current locale strings)
Code:
gawk -v e='%a %d-%m-%Y' '
function regdt(str) {
c=substr(str, 1, 1)
ret = "("
switch(c) {
case "a":
ret = ret "[A-Z][a-z]{2}"
break
case "d":
ret = ret "[0-9]{2}"
break
case "m":
ret = ret "[0-9]{2}"
break
case "Y":
ret = ret "[0-9]{4}"
break
default:
print "Unsupported date fmt string: " c
}
ctype[capture++] = c
return ret ")" substr(str, 2)
}
function verify(i,dow,day,month,year) {
for(v in ctype) {
value=got[++i]
switch(ctype[v]) {
case "a":
dow = value
break
case "d":
day = value + 0
if (day < 1 || day > 31)
return "Illegal dom: " day
break
case "m":
month = value + 0
if (month < 1 || month > 12)
return "Illegal month num: " month
break
case "Y":
year = value + 0
if (year < 1900 || year > 2200)
return "Illegal year: " year
break
}
}
if(day == 0 || month == 0 || year == 0)
return "Must have day, month and year"
dt=mktime(year " " month " " day " 00 00 00")
if (day != strftime("%d", dt))
return " Illegal day for month"
if (dow != "" && strftime("%a", dt) != dow)
return " Wrong dow " dow " should be " strftime("%a", dt)
return ""
}
BEGIN {
vals=split(e, vl, "%");
for(i=1; i<=vals; i++)
if (i==1) expr = vl[i];
else expr=expr regdt(vl[i])
print "Regexp is " expr
}
{
print "String: " $0
if(match($0, expr, got)) {
erstr=verify()
if(erstr == "") print " Date OK"
else print " " erstr
} else
print " Date doesnt match " e " format"
}
' infile
Infile is
Code:
Sat 29-12-2018
Thu 10-02-2019
Fri 30-02-2019
Wed 29-15-2018
Tue 2/2/1900
Result:
Code:
Regexp is ([A-Z][a-z]{2}) ([0-9]{2})-([0-9]{2})-([0-9]{4})
String: Sat 29-12-2018
Date OK
String: Thu 10-02-2019
Wrong dow Thu should be Sun
String: Fri 30-02-2019
Illegal day for month
String: Wed 29-15-2018
Illegal month num: 15
String: Tue 2/2/1900
Date doesnt match %a %d-%m-%Y format
Last edited by Chubler_XL; 06-13-2019 at 11:39 PM..
Reason: Some typos and formatting
These 2 Users Gave Thanks to Chubler_XL For This Post:
1) i am trying to print the timeframe of a log file using the start/end date/time being extracted from the file itself. my problem is how to convert the following numeric date format to the one that i prefer:
Start time: 0204161129
End time : 0204171431
into:
Start time: Apr 16 2002... (6 Replies)
I have a file named "suspected" with series of line like these :
{'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''}
{'protocol': 17, 'service': 'BitTorrent... (3 Replies)
I want to append current date and time to a file name like filename_090920091210. If I use filename_`date +%d%m%Y%T`, the ouput is filename_0909200912:10:33.
How to format this as filename_090920091210.
Please let me know.
Thanks in advance. (3 Replies)
Frens,
I have a logfile which has got manier things in it and somewer in that file i have a date in mm/dd/yyyy hh:mm format....
I just need to get the date and time nothing else....
i have searched in this forum for it but got only date in mm-dd-yyyy format..
but i need to search for... (10 Replies)
Hello,
I am trying to show today's date and time in a better format than ‘date' (Using positional parameters). I found a command mktime and am wondering if this is the best command to use or will this also show me the time elapse since 1/30/70? Any help would be greatly appreciated, Thanks... (3 Replies)
Hello All,
I have a requirement to convert a 12 hour format to 24 hour time format and the sample input /out put is below
Input Time format : Nov 2 2011 12:16AM
Out Put Format : Nov 2 2011 0:16
Input : Nov 2 2011 4:16PM
Out Put: Nov 2 2011 16:16
I have done this using a... (6 Replies)
Hello,
I have date format as shown in red color below
Fri, Sep 12, 2012 08:38:05 PM
Can anyone help me with command to change this format to yyyy-mm-dd and HH:MM:SS
Note: Time should be in 24 hours format
I really appreciate your help!!
Thanks,
Elavarasan (4 Replies)
Hi, I am extracting a date string from the source file like this :
06/05/2014 16:04:00
I want to change it to 05-JUN-14 04.05.00.000000000 PM
I basically store the date in a variable. I got solutions to change date in dd-mmm-yyyy format using tr but I guess it works only with the "date"... (8 Replies)
How can I validate if time (HH:MM:SS) argument is valid? I got this from web but I can't modify it to exit the script if the time argument is invalid.
echo $1 | awk -F ':' '{ print ($1 <= 23 && $2 <= 59 && $3 <= 59) ? "good" : "bad" }'
ex:
./script.ksh 12:34:21 = okay
./script.ksh... (10 Replies)