Script math calculation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script math calculation
# 1  
Old 06-22-2010
Question Script math calculation

Hi Gurus,

I'm currently using HP-UX B.11.23.

I've a simple calculation script which performs the task below.

-> echo "240021344 / 1024 /1024" | bc
Output: 228

240021344 is KB value.

When I tried to perform the same calculate in Ms Excel, it produces a different result: 228.9021912.

Is it possible to perform this calculation and generate this output: 228.90 (2 decimal formats) ?

Would appreciate for any of your advice and help.

Thanks.


Regards,
Peter
# 2  
Old 06-22-2010
Hi.

Try:

Code:
$ echo "scale=2; 240021344 / 1024 /1024" | bc
228.90

# 3  
Old 06-22-2010
Hi scottn,

Thank you for your response.

I've tried your method and it's working on a bigger figure and having figure with smaller value as shown below:

Code:
echo "scale=2; 240021344 / 1024 /1024" | bc

Output: 228.90

Code:
echo "scale=2; 130256/ 1024 / 1024" | bc

Output: .12
-> Should be: 0.12

Code:
echo "scale=2; 1043064 / 1024 / 1024" | bc

Output: .99
-> Should be: 0.99

Are you able to advice on this issue?

Thanks a lot.



Regards,

Last edited by Scott; 06-22-2010 at 06:16 AM.. Reason: Please use code tags
# 4  
Old 06-22-2010
Code:
awk '{ printf("%.2f\n", ( 130256 / 1024 / 1024) ) }'


Last edited by Scott; 06-22-2010 at 06:16 AM.. Reason: Please use code tags
# 5  
Old 06-22-2010
Hi busyboy,

Thanks for your repsonse.

However, when I tried your method, it does not show anything.

Code:
awk '{ printf("%.2f\n", ( 130256 / 1024 / 1024) ) }'

Is there something wrong?

Please kindly advice.

Thanks.


Regards,
Peter
# 6  
Old 06-22-2010
Hi, you need the BEGIN tag:

Code:
 awk 'BEGIN{ printf("%.2f\n", ( 130256 / 1024 / 1024) ) }'

This User Gave Thanks to Klashxx For This Post:
# 7  
Old 06-22-2010
Should be:
Code:
awk 'BEGIN{printf("%.2f\n", 130256 / 1024 / 1024)}'

or:
Code:
echo "130256 1024 1024" | awk '{ printf("%.2f\n", $1 / $2 / $3)}'

This User Gave Thanks to Franklin52 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script for solving the math question

Can such Puzzle solve through UNIX script? if yes, what could be the code? This has been solve in C language. we were trying to solve this through shell but could not because of not able to pass 1st argument with multiple value. we are not expert in unix scripting. Below is the puzzle John is a... (4 Replies)
Discussion started by: anshu ranjan
4 Replies

2. Shell Programming and Scripting

Math calculation over shell

Hi I am trying to calculate the rate at which something is happening. I have 2 files- a1 and b1. I want to calculate something like this ((wc -l a1)/(wc -l a1 + wc -l b1))*100 over a loop for different a and b. Is this possible, help me out fellas. Thanks a lot :) (5 Replies)
Discussion started by: jamie_123
5 Replies

3. Shell Programming and Scripting

math calculation for a txt file

Hi All, I have a text file which is only one column. I want to multiply all these values in file1.txt by 0.01 and get the output.txt file1.txt 65 85 90 ... output.txt 0.65 0.85 0.90 ... Thanks. Please use code tags when posting data and code samples! (2 Replies)
Discussion started by: senayasma
2 Replies

4. Shell Programming and Scripting

time calculation in ksh script

I"m trying to calculate the duration of of backup within a ksh shell script but I get an error. #!/bin/ksh STTIM=`date '+%T'` EDTIM=`date '+%T'` .... .... echo "DURATION OF BACKUP: $((EDTIM - STTIM))" (5 Replies)
Discussion started by: Bperl1967
5 Replies

5. Shell Programming and Scripting

A Math problem using shell script

Have a bit complicated math query .. Basically i am given a number which is > 50 .. I am suppose to find the calculation to get a number which is equal or more than the input number and is also a multiple of any number between 20 - 30 . For example . Input number is 60 . Now 20x3 =60 ... (2 Replies)
Discussion started by: greycells
2 Replies

6. Shell Programming and Scripting

Date calculation script

hello! I need a date calculation script that need to do that: ./date.sh 20090312 and the script need to give me which day is it for example monday friday or what else! can anyone help me?? its really urgent :S thx the help! (7 Replies)
Discussion started by: impish
7 Replies

7. Shell Programming and Scripting

Modulo calculation in shell script

hi i am writing a progrm to print the even numbers and the code which i am following is as follows #!/usr/bin/bash echo "enter a number a" read a if then echo "the number is even" else echo "the number is odd" fi ~ what is the mistake i am doing ...please tell me (3 Replies)
Discussion started by: harjinder
3 Replies

8. UNIX for Dummies Questions & Answers

Can Any help me with the math on this shell script?

Develop a grade calculating program. This program will process all students in the file. This program should neatly display each field of each student's record *and* adds the following items: Course Average and Letter Grade. The course average is calculated by the following weights: 50% for quiz... (7 Replies)
Discussion started by: knp808
7 Replies

9. Shell Programming and Scripting

script execution time calculation

I am writting a script in the ksh shell and am trying to find a way to report the total execution time of the script without requiring the user to specify the time function when executing the script. Does anyone have any examples they have used. I have been setting up two date variables (one at... (9 Replies)
Discussion started by: johnsonbryce
9 Replies

10. Shell Programming and Scripting

Math calculation help

Hi, I wrote this script awk -F"\t" '{if ((($1 == 586) || ($1 == 68030)) && (($2/1024) < 512)) print $0"\t"(512-($2/1024))"\t"(512-($2/1024))/256}' pcs.txt But I want from the calculation in red to get rid of the decimal part. Like instead of 1.75 to keep only 1.Please somebody tell me what... (4 Replies)
Discussion started by: sickboy
4 Replies
Login or Register to Ask a Question