The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 03:37 PM
yet another awk field syntax question prkfriryce Shell Programming and Scripting 4 03-22-2007 01:13 PM
question on egrep rohitv UNIX for Dummies Questions & Answers 10 05-25-2006 05:29 PM
help with egrep syntax Gerry405 UNIX for Dummies Questions & Answers 3 08-06-2005 02:01 PM
awk syntax question hcclnoodles Shell Programming and Scripting 2 10-28-2004 12:45 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-18-2007
DKNUCKLES DKNUCKLES is offline
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!
  #2 (permalink)  
Old 11-18-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,847
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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0