LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-19-2009, 08:57 AM   #1
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Rep: Reputation: 15
Bash maths question


Hi all


I find myself a bit stuck again.

I got a text file containing something along the lines of this:

Code:
0.0
0.0050
0.0
0.034
2.885
0.0090
0.0
1.203
0.0
0.013
4.065
0.0030
0.0060
0.0
0.0060
4.557
6.423
6.39
0.0070
0.0010
3.226
0.0
0.0
0.0040
6.117
0.0
0.228
2.791
1.563
6.456
0.0
6.317
0.0
0.0050
1.237
0.02
1.563
1.468
0.0020
0.0010
0.0
2.832
6.425
1.561
0.0
4.489
I'm trying to write a simple bash script that will give me a total of all the values. The machine I'm trying to do this on doesn't have bc installed, so I'm afraid I'm limited to taking shortcuts in the script only.

Here's what I got so far:

Code:
tail -n 50 /path/to/file.log | awk {'print $3'} | sed -n '/Rsp/!p' | sed -n '/--/!p' | sed -n '/\//!p' | sed -e '/^$/d' > /path/to/values.log

while read num
do
sum=$(($sum + $num))
echo "$sum"
done < <(cat /path/to/values.log)
clear
echo "$sum"

Problem is it doesn't seem like the .[0-9] is liked very much by the while do statement.

Is there a way I can get this to work with [0-9].[0-9]* values?


Thanks in advance


EDIT: My first language isn't English, turns out .something is floating point math? And BASH can't do floating point math? That kind of sucks, especially since I do not have bc at my disposal...

Last edited by kinetik; 01-19-2009 at 09:12 AM.
 
Old 01-19-2009, 09:29 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Does that machine have awk installed, if so, you can do it quite easily.

Code:
bash-3.1$ echo 12.344 123.344 8585.4848 | awk '{ printf("%0.5f\n",$1 + $2 + $3) }'
8721.17280
 
Old 01-19-2009, 09:42 AM   #3
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by kinetik View Post
And BASH can't do floating point math? That kind of sucks, especially since I do not have bc at my disposal...
You could use python (if you have it) - it is perfectly suitable for your task.
 
Old 01-19-2009, 10:07 AM   #4
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
First of all, thanks ErV and H_TeXMeX_H for the assists, much appreciated.

I'm first going to try to tackle this in another way and will keep you posted whether that worked or not. If it doesn't work (and I doubt it will), I'll definitely fire some more questions in here.

Thanks again for the help so far.
 
Old 01-19-2009, 10:33 AM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

And if it doesn't work, then perhaps you could tell us what you have available among the likely suspects such as awk, python, perl, ksh, zsh, etc ... cheers, makyo
 
Old 01-19-2009, 11:14 AM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
If your numbers are all of the form [0-9]*.[0-9]{0,4} you could change them to integers by concatenating "0000"to the input number, truncating the result four digits after the "decimal point" and then removing the "decimal point." Then the total would be 10000 * the desire total, and you could jam in a "decimal point" in the total if you need to do so.

Since you seem to have sed installed, implementing this "convert input to 10000*(input)" scheme should be fairly simple to implement.

With a little effort you could implement you whole "find the total"in sed, although it might be tedious. Look at the "Incrementing a number" example in the "Sample scripts" section of info sed for an example of how n <- n+1 can be done with sed. (Note: I don't recommend using sed, just saying it's possible.)
 
Old 01-20-2009, 02:57 AM   #7
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Hi Makyo and PTrenholme


I'm glad to say I got it working (sort of) by using sed. I think the system does have Python, awk, ksh, zsh etc. installed, just not bc for some daft reason.

I doubt this is going to be of any use to anyone else in future, but here's how the script turned out:

Code:
## totala.sh
tail -n 50 /path/to/file.log | awk {'print $3'} | sed -n '/^[0-9]/p' | sed "s|0$||g" | sed -n '/\.$/!p' | sed -e '/^$/d' | sed "s|\(\.[0-9]$\)|\10|g" | sed "s|\(\.[0-9][0-9]$\)|\10|g" | sed "s|^0\.||g" | sed "s|^0||g" | sed "s|^0||g" | sed "s|^0||g" | sed "s|\.||g" > /path/to/values.log

while read num
do
sum=$(($sum + $num))
echo "$sum"
done < <(cat /path/to/values.log)
clear
echo "$sum" > /path/to/values1.log

OK, if I got this right, here's what the sed commands in the script does:

sed -n '/^[0-9]/p' <-- Only print lines beginning with numbers
sed "s|0$||g" <-- Replace any ending 0 with nothing
sed -n '/\.$/!p' <-- Don't print anything ending with a "." And this one doesn't look quite right now that I look at it
sed -e '/^$/d' <-- Take out any blank lines
sed "s|\(\.[0-9]$\)|\10|g" <-- Adds a 0 at the end of numbers like .4
sed "s|\(\.[0-9][0-9]$\)|\10|g" <-- Adds a 0 at the end of numbers like .44
sed "s|^0\.||g" <-- Removes "0." from all the numbers Doesn't look quite right either
sed "s|^0||g" <-- Removes any 0 at the beginning of the numbers
sed "s|^0||g" <-- Same as above, just does it again to be safe
sed "s|\.||g" <-- Removes the .



Anyhoo, I made a second script that will run the above script, let's call it totalb.sh:

Code:
/path/to/totala.sh > /dev/null 2>&1
cat /path/to/values1.log | sed ':a;s/\(^\|[^0-9.]\)\([0-9]\+\)\([0-9]\{3\}\)/\1\2,\3/g;ta'

I think the script works OK-ish for a file containing multiple values, but the problem comes in when it only contains things like 0.1 and 0.009 or something.

Thanks to everyone that helped me out (colucix, H_TeXMeX_H, ErV, makyo, PTrenholme), I really appreciate it. This chapter isn't over yet though, so if you don't mind I'm probably going to fire some Python and awk questions your way soon.
 
Old 01-20-2009, 03:21 AM   #8
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by kinetik View Post
I'm trying to write a simple bash script that will give me a total of all the values. The machine I'm trying to do this on doesn't have bc installed, so I'm afraid I'm limited to taking shortcuts in the script only.

EDIT: My first language isn't English, turns out .something is floating point math? And BASH can't do floating point math? That kind of sucks, especially since I do not have bc at my disposal...

.....

Code:
/path/to/totala.sh > /dev/null 2>&1
cat /path/to/values1.log | sed ':a;s/\(^\|[^0-9.]\)\([0-9]\+\)\([0-9]\{3\}\)/\1\2,\3/g;ta'
Look, it's not that complicated, here's what I would do:

Code:
#!/bin/awk -f
BEGIN {
# initialize total
    total=0;
}
{
# this code is executed once for each line
# add field 1 to the total for each line
    total+=$1;
}
END {
# end, now output the total
    print "The total is: ", total;
}
I've checked it and it prints out the right total. If you want to format the output use printf instead of print.

I used a script from here to start off:
http://www.grymoire.com/Unix/Awk.html
If you need more help, check there.

Last edited by H_TeXMeX_H; 01-20-2009 at 03:23 AM.
 
Old 01-20-2009, 04:08 AM   #9
kinetik
Member
 
Registered: Dec 2005
Location: The most beautiful city in the world.
Distribution: Mostly RedHat. Also Suse, Ubuntu, PHLAK etc.
Posts: 149

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by H_TeXMeX_H View Post
Look, it's not that complicated, here's what I would do:

Code:
#!/bin/awk -f
BEGIN {
# initialize total
    total=0;
}
{
# this code is executed once for each line
# add field 1 to the total for each line
    total+=$1;
}
END {
# end, now output the total
    print "The total is: ", total;
}
I've checked it and it prints out the right total. If you want to format the output use printf instead of print.

I used a script from here to start off:
http://www.grymoire.com/Unix/Awk.html
If you need more help, check there.
You sir, I owe you a beer. Thanks H_TexMeX_H
 
Old 01-20-2009, 04:43 AM   #10
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
No problem. And remember that in most programming or scripting languages that are sane, when things start to get really complicated, obscure, and unreadable (as illustrated above) then perhaps you have taken a wrong turn somewhere, perhaps you are missing a simpler alternative.
 
  


Reply



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
Verizon Maths acid_kewpie General 9 12-10-2006 09:37 AM
Suggestions for Maths Software andrewlkho Linux - Software 3 04-11-2005 09:44 PM
please help me do maths :) darkRoom General 36 04-11-2005 01:03 PM
problem with maths redduck666 General 7 02-12-2005 02:33 PM
And some maths :-P Mathiasdm General 7 11-11-2004 03:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:17 PM.

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
Open Source Consulting | Domain Registration