[NTLUG:Discuss] verify that a text string is a valid date.

Robert Citek rwcitek at alum.calberkeley.org
Thu Feb 2 02:09:22 CST 2006


On Feb 1, 2006, at 12:58 PM, Richard Geoffrion wrote:
> 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.

It's called string manipulation.  Based on your explanation here's  
some sample code:

( set -x
cat <<eof | tee ex1.txt
2006-02-01 this is a valid date
with a second line.
eof
cat <<eof | tee ex2.txt
not valid 2006-02-01
with a second line.
eof
set $(dd if=ex1.txt bs=1 count=10 2> /dev/null | tr '-' ' ')
echo $1, $2, $3
set $(dd if=ex2.txt bs=1 count=10 2> /dev/null | tr '-' ' ')
echo $1, $2, $3
)

And here's what the output looks like:

+ cat
+ tee ex1.txt
2006-02-01 this is a valid date
with a second line.
+ cat
+ tee ex2.txt
not valid 2006-02-01
with a second line.
++ dd if=ex1.txt bs=1 count=10
++ tr - ' '
+ set 2006 02 01
+ echo 2006, 02, 01
2006, 02, 01
++ dd if=ex2.txt bs=1 count=10
++ tr - ' '
+ set not valid
+ echo not, valid,
not, valid,

Hope this helps.  Post back if you have questions.  Also, be sure to  
let us know what you tried and how it worked for you.

Regards,
- Robert
http://www.cwelug.org/downloads
Help others get OpenSource software.  Distribute FLOSS
for Windows, Linux, *BSD, and MacOS X with BitTorrent




More information about the Discuss mailing list