Question:How to validate date column in a file using regular expression in awk ?? My code doesn’t seem to be working with awk.
\zsequences are not valid regex operators (a quick web search doesn’t show these as valid regex operators in a couple others flavors of
awkthough by no means an exhaustive search).
I’d suggest replacing the
[[:digit:]]; as for the
\zyou could try
One other issue is the use of
.as a wildcard match in the time component; if you know all times will use a colon (
:) as a delimiter then I’d use an explicit colon.
Rolling these changes into the current code (and fixing a couple cut-n-paste/syntax issues):
- obviously (?) this code assumes a specific date/time format and thus …
- this code will not match on other valid date/time formats (eg, won’t match on
- the use of
[0-9]opens you up to matching on strings that are not valid dates and/or times, eg, this code will match on
99:99:99); OP can address some of these by limiting the series of digits that can be matched in a given position (eg,
[0-2][0-9]for hours) but even this is problematic since
29will match but is not a valid hour
- as alluded to in comments … validating dates/times is doable but will require a good bit more code (alternatively run a web search on
bash awk validate dates timesfor additional ideas)
If you have better answer, please add a comment about this, thank you!