Date & NUmber Validation Query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date & NUmber Validation Query
# 1  
Old 12-15-2004
Question Date & NUmber Validation Query

Hi

Do you have any pointers how to validate numbers (not to contain alphabets and special characters) and date(MM/DD/YYYY) format.



I used following regular expression to validate integer, which is not working in the default shell:


nodigits="$(echo $testvalue | sed 's/[[:digit:]]//g')"

if [ ! -z $nodigits ] ; then
echo "Invalid number format! Only digits, no commas, spaces, etc." >&2
return 1
fi


Thanks
Alok
# 2  
Old 12-15-2004
I'd use grep
Code:
$ echo "12/31/2004" | grep '^[0-1][0-9]/[0-3][0-9]/[0-9]\{4\}$' >/dev/null 2>&1
$ echo $?
0
$ echo "some text" | grep '^[0-1][0-9]/[0-3][0-9]/[0-9]\{4\}$' >/dev/null 2>&1
$ echo $?
1

You can then check $? and act accordingly.

You can extend this example to simple numerical verification...
Code:
$ echo "193912391293" | grep '^[0-9]*$'

You get the idea....

Cheers
ZB

Last edited by zazzybob; 12-15-2004 at 10:48 AM..
# 3  
Old 01-05-2005
What happens when you use this command?

echo "12/31/2004" | grep '^[0-1][0-9]/[0-3][0-9]/[0-9]\{4\}$' >/dev/null 2>&1

I used it but did not use echo $? afterword and now I'm afraid to use it on /dev/null again.

I actually used the above command but replaced the /dev/null with Test1 a file in my test directory. It erased everything in that file and replaced it with 12/31/2004.

What can I do about my /dev/null? I don't what that to be erased. But also with my test file I wanted to change the format of the date for every occurance of 12-31-2004 or 12/31/2004 to 2004-31-12 would the above command work on that with a little more tweaking?

thanks in advance!
# 4  
Old 01-05-2005
Don't worry about damaging /dev/null - unless you're logged in as root and issue rm /dev/null nothing bad will happen!

The > redirection operator will erase anything in a file before outputting to it - use >> if you want to append.

If you want to change the format to delimit the date components with "-" just change the command to

echo "12-31-2004" | grep '^[0-1][0-9]-[0-3][0-9]-[0-9]\{4\}$' > myfile # or wherever

If you had the date in a variable, say DATE, then you can check the format of DATE with

Code:
echo "$DATE" | grep '^[0-1][0-9]-[0-3][0-9]-[0-9]\{4\}$' > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
   echo "Date correctly formatted"
else
   echo "Date incorrectly formatted"
fi

If you have a something along the lines of
Code:
DATE="12/31/2004"

and you want to change the format, try
Code:
echo "$DATE" | grep '^[0-1][0-9]/[0-3][0-9]/[0-9]\{4\}$' > /dev/null 2>&1
if [ "$?" -eq "0" ]; then
   echo "Date correctly formatted"
   echo "$DATE" | tr '/' '-'
else
   echo "Date incorrectly formatted"
fi

And the "/" will be substituted for "-"

If this isn't what you want please post exact requirements.

Cheers
ZB
# 5  
Old 01-05-2005
Thank You so much for the quick reply.
And thank God I wasn't root today.
Anyway, the code looks like it will work
but I haven't tried it yet.
I'll let you know what happens.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Date validation

Hi folks, I new to shell script . I want to know how to validate a String as valid date example: 20150712 ---> valid date 20160524-->valid 201605T12-->invalid date 12341234--->invalid date we need to valid string( yyyymmdd) to date in SunOS 5.10 please give some idea to validate... (9 Replies)
Discussion started by: srinadhreddy27
9 Replies

2. UNIX for Dummies Questions & Answers

Date Validation not working

Hi Experts, I have a date validation script in that i will validate the date for a given format and search in the logs for that date. The script logic is very simple like below. Validate_Date() { is_valid=1 while do date_format=$(date "+$1") echo -e "Please enter the $2 date like... (6 Replies)
Discussion started by: senthil.ak
6 Replies

3. Programming

Date validation in mysql

Hi All, We need to create the custom function to pass the parameter is date.if it is valid return 1 else 0 return should be 1 select is_date('2012-09-17'); return should be 0 select is_date('2012-79-17'); Thanks (2 Replies)
Discussion started by: bmk
2 Replies

4. Shell Programming and Scripting

Date validation

File contains below data,how to validate the date using awk command or any command. date formate is fixed as "YYYYMMDD" test1|20120405 test2|20121405 output should be: test1|20120405 Thanks (2 Replies)
Discussion started by: bmk
2 Replies

5. Shell Programming and Scripting

Date validation

Hi, I have a script which runs on specific sunday. If that script runs on the sunday i want to execute another script on following wednesday. I have a log for that server. My wednesday scripts needs to check the sunday run log timestamp and if it matches it should run. Please help. Thanks,... (1 Reply)
Discussion started by: Krrishv
1 Replies

6. Shell Programming and Scripting

Date Validation in unix

I have a script which is take date as parameter sh abc.sh <2010-02-01> #!/sh/bin my_date=$1 #Here i want to two diffrent dates ## 3 Days before ##date14query=$mydate - 4 (it will be 2010-01-28) ##date24query=$mydate +4 (it will be 2010-01-05) #Please Help (3 Replies)
Discussion started by: pritish.sas
3 Replies

7. Shell Programming and Scripting

Date Validation

the user have to input the date format in mmddmmhhyyyy (month,date,minutes,hour,year) i want a shell script to check whether the user has properly input in the above said manner. kindly advice (2 Replies)
Discussion started by: vkca
2 Replies

8. Shell Programming and Scripting

Date Validation:

Hi I have below file with 3 rd column as date ....i want to make 3 column to mm/dd/yyyy . in the below file 2 row date is like 1/23/1994 so i want to append '0' to month i,e. 01/23/1994 and in the 3 row date is like 6/4/1994 ---so i want this to 06/04/1994 Source:... (1 Reply)
Discussion started by: satyam_sat
1 Replies

9. Shell Programming and Scripting

Date Validation again

I want a sample Date validation script using if loops. The script should first compare "year".If the year is lesser than the current year,It should go for "month" checking. I have the script that splits the date into year,month and date.I want only the checking part. My if loop checking is not... (4 Replies)
Discussion started by: dave_nithis
4 Replies

10. Shell Programming and Scripting

Problem with Date validation

Hi All, I've to validate a date input for the format YYYYMMDD. The input should be of 8 digits with only numeric values. I tried the following echo $1 | grep '^\{8}$/p' if then echo "Valid" else echo "invalid" I get the error, grep: RE error 16: Bad number. Any help... (3 Replies)
Discussion started by: sumesh.abraham
3 Replies
Login or Register to Ask a Question