
How do I strip leading zeroes for math in a shell script?I have a file containing lines of data that are amounts padded with leading zeros, similar to the snip below. 0000000004 Using a shell script, how can I add up a column of numbers contained in lines when BASH interprets the numbers as octal when I do in-line math? What an interesting puzzle you present to me! My first thought was that there's a cool way I can solve this using a shell function, one that stripped a single leading zero then compared its results to the pre-truncated version of the value, until they matched (e.g., all the leading zeroes were deleted). It'd look something like this: function stripzeroes This would then be called as stripzeroes "0000000003434" (or whatever value you'd read in from the data file) and the result would be returned as the value newvalue without the leading zeroes. Nice solution, classic little shell script function, but there's one problem. With the right regular expression, you can strip all the leading zeroes from your data fields with just a few characters: valueWithoutZeroes="$(echo $valueWithZeroes | sed 's/0*//')" That's right, but using the proper regular expression to the sed command, you can very easily strip off all the leading zeroes, making your math far, far simpler and all without complicating your shell script with useless functions! Hope that helps you out.
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.
Categorized:
Shell Script Programming
(Article 6487)
Tagged: shell script programming, unix programming, wicked cool shell scripts Previous: How do I get a cool MySpace URL? Next: How do I configure a DMZ on my local LAN? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. Not to be confused with this Dave Taylor, I'm the Dave Taylor from the game industry, and I'm visiting because my uncle Paul Taylor thought I might have been doing this as I share your penchant for answering things. It's a neat site, and I think you're providing a darned handy service, btw. I think there's a small bug with both solutions. You've got an issue if one of the lines reads thusly: 0000000000 You'd want to strip that down to just "0", but both algorithms you use basically delete the line, which could cause some pretty serious issues, depending on what's eating the output. Darn, you're right, Dave! Using the 'sed' solution, simply add a test after the conditional: if [ -z "$valueWithoutZeroes" ] ; then that should do the trick! Posted by: Dave Taylor at April 28, 2006 12:44 AMI figure out a much simpler way of doing it: newValue=`expr $oldValue + 0` Note that this only works with "data that are amounts", as the question says. Posted by: Alejandro Biasin at November 2, 2006 12:04 PMHere's my usual solution, pretty much equivalent to Alejandro's use of expr:
I'm fairly new to this and I do follow the logic. However, I have problems trying to tie it all up together. For instance I have a file 'benshrs' with figures I need to add up in columns 21 thru 27. Some are all zeroes. What's the best way (complete script)that combines a. the cut -c 21-27 b. strip the leading zeroes except when it is all zeroes, c. add up the figures in do ... done loop, and lastly print the final sum. My crude attempt (below) has failed miserably! I'm not surprised this isn't working, Ben: you can't use floating point / real numbers in a shell script, so even your initial assignment of hrs_total=0.00 is going to fail before you even get into the for loop. It's possible that you need to do this in awk or, better, Perl, to get it working. Posted by: Dave Taylor at May 7, 2007 7:55 PMNot that your option wouldn't work, Dave, but I'm curious if it isn't a bit of an overkill solution. Why not simply employ something like: $ typeset -i example1="00001" This approach is available to both ksh and bash, and would immediately strip out the zeroes or otherwise negate the value to "0" if there were some alpha values. Same applies to a fully zero-filled value. This eliminates the need for any additional function definitions at all...but is this a matter for portability? Sure it restricts the math to integers, but sed would totally skew the numbers if it was used to massage the value. [Your page doesn't seem to restrict the discussion to a particular shell that I can see.] Cool. Never knew about the "typeset" built-in to the shell. Very interesting solution, thanks! Posted by: Dave Taylor at May 25, 2007 8:13 PMThe typeset -i (or declare -i in more recent versions of bash) doesn't work if the value is greater than 7 -- it's interpreted as an octal number. $ declare -i x=009 The most reliable solution so far is using expr + 0 I think. Posted by: Alexandre H at October 3, 2007 9:02 AMI have a lot to say, but ...
I do have a comment, now that you mention it!
|
Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|