ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You did not post any follow up to that thread, so we don't know if you resolved the issue, following the given suggestions.
Anyway, in sed you have to use a pattern reference. That is you have to embed in escaped parentheses the part of the pattern you want to remember, then use \1 as the first pattern, \2 as the second pattern and so on. Your line should be modified as follows:
Regarding your original question in the previous thread: to sum floating point numbers without bc you can use awk (as already suggested by H_TeXMeX_H) or even python (by ErV). In awk I would do something more efficient as:
Code:
awk '{sum+=$0}END{print sum}' file
You can try awk also as alternative to the sed command above:
Code:
echo 15347 | awk '{printf "%6.3f\n", $0/1000}'
this will give more flexibility about the input number and the output format (you can chose how many digits before and after the dot).
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149
Original Poster
Rep:
Quote:
Originally Posted by colucix
You did not post any follow up to that thread, so we don't know if you resolved the issue, following the given suggestions.
Indeed, and I'm terribly sorry about that.
That's my hard-headedness coming out as I already had a sort of idea how I wanted to tackle this without resorting to another language.
I'm crazy about sed, and I really wanted to be able to tackle my first problem by getting this question answered first. Maybe even learn something more about sed in the process (which I just did by reading your advice).
The plan:
Take out the decimal characters, add the numbers together, then just insert the decimal back in three numbers from the end.
No idea if it's going to work, but here's hoping.
Thanks for your help so far as well, much appreciated.
Indeed, and I'm terribly sorry about that.
That's my hard-headedness coming out as I already had a sort of idea how I wanted to tackle this without resorting to another language.
No need to sorry... really. Furthermore I noticed it was a very recent thread, just after I posted to this one.
Quote:
Take out the decimal characters, add the numbers together, then just insert the decimal back in three numbers from the end.
Theoretically it should work. Practically it would be very difficult to take in account the different number of digits in each value using sed. Again I'd suggest a more efficient way using awk, since it can do floating point calculations in a straightforward manner.
Interesting, I didn't know it could be put on one line like that. Still, it's not that readable, where is sum initialized ?
Awk initializes variables to 0, if not given a specific value. To be sure, you can always add the begin section:
Code:
awk 'BEGIN{sum=0}{sum+=$0}END{print sum}' file
but it is not really necessary.
Here is the relevant section from the official AWK programming guide (section 5.3.1)
Quote:
Variables in awk can be assigned either numeric or string values. The kind of value a
variable holds can change over the life of a program. By default, variables are initialized to
the empty string, which is zero if converted to a number. There is no need to “initialize”
each variable explicitly in awk, which is what you would do in C and in most other traditional
languages.
Last edited by colucix; 02-03-2009 at 05:31 AM.
Reason: Added reference
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.