[NTLUG:Discuss] verify that a text string is a valid date.
steve
sjbaker1 at airmail.net
Wed Feb 1 21:43:54 CST 2006
Richard Geoffrion wrote:
> I'm trying to do something but I don't know what it is called.
>
> I want to check to see if the first 10 characters of a file is a vaild
> date...eg 2006-02-01 and if it IS..then I want to continue the process.
>
> What am I trying to do in bash(or other utility terms)? I don't know
> the name of what I'm trying to do so I'm finding it difficult to search.
If you had to do it in bash only, I'd suggest using 'head' to chop
out the first 10 characters and store it in a shell variable:
DATE=`head --bytes=10 file`
(The quotes around the command are back-quotes).
Or, I guess you could chop out the year, month and day separately
using 'head' and 'tail' together:
YEAR=`head --bytes=4 file`
MONTH=`head --bytes=7 file | tail --bytes=2`
DAY=`head --bytes=10 file | tail --bytes=2`
Now you can use bash's aritmetic testing to check that the ranges
of the numbers are reasonable. This doesn't check that the '-'
symbols are correctly placed - and it's a pain to check that you
didn't get ABCD-12-34...but it's a start.
More information about the Discuss
mailing list