LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-24-2012, 05:55 PM   #1
bcyork
LQ Newbie
 
Registered: Dec 2010
Location: Chicago, IL
Distribution: Linux Mint 11 / Debian 6
Posts: 29

Rep: Reputation: 1
Currency / Dollar amount variable Expression format


Hey, I am writing a script that needs to account for varying dollar amounts. I'm a bit stuck on the comma part and can't figure out how to make it accommodate for a possible comma.

Here is what works if the dollar amount is over a thousand and has a comma in it.

[0-9]*[,][0-9]*.[0-9][0-9]

Here is what works if the dollar amount is under a thousand without a comma in it.

[0-9]*.[0-9][0-9]

My question is how can I write this so that it will see the comma as a possibility but not an absolute when sorting through the data to extract it. If I use the one without a comma and the script will ignore any number over a thousand.

Thanks for the help! I searched and perhaps missing something on creating expressions.

-Brian
 
Old 12-24-2012, 06:54 PM   #2
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 6,869

Rep: Reputation: 1247Reputation: 1247Reputation: 1247Reputation: 1247Reputation: 1247Reputation: 1247Reputation: 1247Reputation: 1247Reputation: 1247
To get specific help, I would suggest you post the programming language you intend to use, bash script, C, python, perl, or any of the numerous other programming languages available. From the info you have posted, it would seem a simple if/else statement would work but posting a little more detail on your script might help someone more familiar with program help you.
 
1 members found this post helpful.
Old 12-25-2012, 02:44 AM   #3
bcyork
LQ Newbie
 
Registered: Dec 2010
Location: Chicago, IL
Distribution: Linux Mint 11 / Debian 6
Posts: 29

Original Poster
Rep: Reputation: 1
Its a bash script
 
Old 12-25-2012, 04:15 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Have a look at this regular expression:
Code:
[0-9]{1,3}(,[0-9]{3})*\.[0-9]{2}
blue part => starts with 0-9, 1, 2 or 3 times
brown part => followed by a comma and 0-9, zero or more times
green part => followed by a dot and 0-9, 2 times

Depending on the command you use the above regexp with, you might need to escape some parts (telling us you use bash isn't enough info).

Last edited by druuna; 12-25-2012 at 04:18 AM. Reason: coma -> comma
 
1 members found this post helpful.
Old 12-25-2012, 08:17 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Yes, give us some context so we can better understand what you're doing. What command(s) are you using, exactly? Please show us some actual code and a few examples of the input and how it needs to match.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
using variable in xpath expression lalitcoep Linux - General 0 01-02-2011 04:00 AM
Complicated VI replacement with variable expression jhwilliams Linux - Software 1 08-17-2007 10:44 AM
Question about a function that takes a variable amount of arguments daYz Programming 5 03-15-2007 02:59 AM
How do I set default Currency , Date format anilbh Fedora 3 07-05-2006 10:35 PM
Is there a currency format with printf() ? LUB997 Programming 1 01-03-2005 09:33 PM


All times are GMT -5. The time now is 02:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration