The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Egrep cheat sheet anywhere? Looking for meaning of egrep -c leelm UNIX for Dummies Questions & Answers 2 01-11-2008 12:37 PM
yet another awk field syntax question prkfriryce Shell Programming and Scripting 4 03-22-2007 10:13 AM
question on egrep rohitv UNIX for Dummies Questions & Answers 10 05-25-2006 02:29 PM
help with egrep syntax Gerry405 UNIX for Dummies Questions & Answers 3 08-06-2005 11:01 AM
awk syntax question hcclnoodles Shell Programming and Scripting 2 10-28-2004 09:45 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-18-2007
Registered User
 

Join Date: Oct 2007
Posts: 12
egrep syntax question

I have a (seemingly) simple question for you guys

I need my script to allow users to enter numbers with either 2 decimal points or with no decimal points.

here's the code I have right now for testing, but I can't for the life of me figure out whats wrong!

(it's been broken up into 2 different grep statements)

decimal=10.500
echo $decimal | egrep -q '[0-9+]' && echo success || echo failure
echo $decimal | egrep -q '[0-9+]\.[0-9][0-9]' && echo success || echo failure

any insight would be greatly appreciated as this has been the cause of much frustration for me!
Reply With Quote
Forum Sponsor
  #2  
Old 11-18-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
With egrep:

Code:
printf "%s\n" "$decimal"|egrep '^[0-9]+(\.[0-9][0-9])?$' >/dev/null && echo OK||echo KO
Or (with not too old versions):

Code:
printf "%s\n" "$decimal"|egrep -q '^[0-9]+(\.[0-9]{2})?$' && echo OK||echo KO
With bash3(and ksh93?):

Code:
[[ "$decimal" =~ ^[0-9]+(\.[0-9]{2})?$ ]]&&echo OK||echo KO
Of course, you should modify it a bit, if you want
.00 and 00. to be considered OK.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0