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

Richard Geoffrion ntlug at rain4us.net
Thu Feb 2 09:17:23 CST 2006


Robert Citek wrote:

>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:
>
>  
>
<snip sample code>

Ok..it took me a second...but I see what you're doing with the sample 
script.  That does extract the date, but it doesn't confirm that it is 
in a `date` format.  Interesting use of dd though.....I'll have to keep 
that around.

Steve!   I'll definately be remembering 'head'.  Thanks!

>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.
>
>  
>
I've added a TODO to my code to do the julian date convert x 2 = 
Validity check...  In the mean time, here is how I'm currently doing 
'some' form of date checking.   This method doesn't prevent 
say....YYYY-DD-MM ( 2006-31-01)... but at least it gets close.

Best viewed in fixed width font....obviously.

<code>
#########################################################################
# Extract the date from the file and move pdf to the proper directory   #
#########################################################################

    #extract the date by cutting bytes 1-10 out of the filename
    NEWDIRNAME=`echo $FILE | cut -b 1-10`

#########################################################################
# Verify that the date for NEWDIRNAME is a potentially valid one        #
#########################################################################

    #TODO: Convert $NEWDIRNAME to a julian date and back again and test 
for validity

    #check the YearMonth delimiter and the MonthDay delimiter
    YM=`echo $NEWDIRNAME | cut -b 5`
    MD=`echo $NEWDIRNAME | cut -b 8`

    #make sure that both YM and MD are dashes
    if [ "$YM" = "-" -a "$MD" = "-" ] ; then

        #Strip the filename of its date and the the IRFANVIEW's 
numbering scheme.
        # then assign it to variable SFILE
        
#################################################################################
        # echo $file to get the name, then cut out everything except for 
bytes 12       #
        # and on.. then reverse what's left...then cut everything except 
for bytes 3    #
        # and on (this gets rid of IRFANVIEW's anoying habit of 
numbering the scans     #
        # with 01 02 03..etc... and of course after you get rid of the 
scan number,you  #
        # have to re-reverse it so that it's back to normal.            
                #
        
#################################################################################
            SFILE=`echo $FILE | cut -b 12- | rev | cut -b 3- | rev`

            #<snip the rest of what happens here>

    else    #assume the date format is bad and move the file into an 
unfiled folder

        #make sure that the unfiled directory exist
        if [ ! -d unfiled ] ; then
            mkdir -p $UNFILED
            chown $OWNER.$GRPOWNER $UNFILED
        fi
                # Set the StrippedFile name so that we remove the 
IRFANVIEW's numbering scheme
                # but leave the invalid date.
                SFILE=`echo $FILE | rev | cut -b 3- | rev `
                #Set quotes around  $SFILE because it hasn't been 'tr'ed 
to convert stupid spaces
                mv $FILE.pdf "$UNFILED/$SFILE.pdf"
    fi
</code>

-- 
Richard





More information about the Discuss mailing list