• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: DATE validation using regex in awk

Resolved: DATE validation using regex in awk

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

How to validate date column in a file using regular expression in awk ?? My code doesn’t seem to be working with awk.
my code
file contents –
expected output

Answer:

In GNU awk the \d and \z sequences are not valid regex operators (a quick web search doesn’t show these as valid regex operators in a couple others flavors of awk though by no means an exhaustive search).
I’d suggest replacing the \d with [0-9] or [[:digit:]]; as for the \z you could try \> or \y.
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):
This generates:
NOTES:
  • 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 2021/12/31)
  • 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/2022 and 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 29 will 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 times for additional ideas)

If you have better answer, please add a comment about this, thank you!

awk bash regex shell
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to compare two text files and and change the sign of the data in powershell?

27/03/2023

Resolved: Java Virtual Machines deleted

27/03/2023

Resolved: PyCharm cannot see my newly compiled .pyc see on import

27/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.