Add non-integers using ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add non-integers using ksh
# 8  
Old 04-27-2007
nawk seems to handle it fine -

awk 'BEGIN{Fraction=1/3;printf("%0.2f\n",Fraction)}'
0.33
awk 'BEGIN{Fraction=2/3;printf("%0.2f\n",Fraction)}'
0.67
# 9  
Old 04-27-2007
awk,

The function works as well when displaying a fraction. However I would like to return the percentage. If possible I would like to use only one funciton to add, subtract, percentage, etc.

calc()
{
awk 'BEGIN {print '"$*"'; exit}'
}

calc "1188 / 1454.07"

Would return: 0.817017
But I would like to get: 81.70
# 10  
Old 04-27-2007
Not only 81.70 but 81.70%

awk 'BEGIN{Fraction=1188 / 1454.07;printf("%0.2f%%\n",Fraction*100)}'
81.70%
# 11  
Old 04-27-2007
awk,

When I try to substitue a vaiable I recieve a sysntax error. Any Ideas?

USED=1188
TOTAL=1454.07

awk 'BEGIN{Fraction=${USED} / ${TOTAL};printf("%0.2f%%\n",Fraction*100)}'

Syntax Error The source line is 1.
The error context is
>>> BEGIN{Fraction=${ <<<
awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.
Syntax Error The source line is 1.
awk: 0602-540 There is a missing } character.
# 12  
Old 04-27-2007
2 solutions

export A=1188
export B=1454.07

awk 'BEGIN{Fraction=ENVIRON["A"] / ENVIRON["B"];
printf("%0.2f%%\n", Fraction)}'

awk 'BEGIN{Fraction='$A' / '$B';
printf("%0.2f%%\n", Fraction)}'
0.82%
0.82%

The first may have issues with some awks, but I prefer it myself.

The latter is more universal, but be sure and not put any spaces before or after the $A and $B. It does not have the have the variables exported.

To me the first is easier to debug, which is why I prefer it.

Neither have error coding - left to you - ie. non-numerics or dividing by zero error.
# 13  
Old 04-27-2007
Works great thanks awk.
# 14  
Old 04-27-2007
A ksh co-process is a very fast solution because a single process can handle all of your calculations. In contrast, the above solution need one one awk process per calculation.

Code:
#! /usr/bin/ksh

bc |&
print -p scale=5

function calc
{
        typeset result
        print -p "$@"
        read -p result
        echo "$result"
}



calc 1.5 / 3
A=1.5
B=3.0

calc $A / $B


$ ./testcalc
.50000
.50000
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing Integers (I think)

Hi, I can't figure out what I'm missing. I'm running a query to see if there are any streams recording on my DVR before starting a scripted update. I'm guessing that it is viewing $VIDEO as a string instead of an int. I've tried everything I saw on google but it still comes back as $VIDEO is... (8 Replies)
Discussion started by: Rhysers
8 Replies

2. UNIX for Dummies Questions & Answers

Strings to integers?

Hi, I'm totally new at this, so help will be appreciated. I have a directory with a bunch of files in it. The files are named xinteger_yinteger_zinteger.vtk (eg, x3_y0_z-1.vtk). I want to read the filenames and then assign the integers to variables that I then can use in expressions. So, for... (6 Replies)
Discussion started by: jhsinger
6 Replies

3. Shell Programming and Scripting

Bash Integers/String

Hy friends, I am newbie to bash scripting, can anyone explain how b=${a/23/BB} # Substitute "BB" for "23". this line converts "b" into string and and "d" into Integer. Thanks in advance (4 Replies)
Discussion started by: Qazi
4 Replies

4. Shell Programming and Scripting

Grep float/integers but skip some integers

Hi, I am working in bash in Mac OSX, I have following 'input.txt' file: <INFO> HypoTestTool: >>> Done running HypoTestInverter on the workspace combined <INFO> HypoTestTool: The computed upper limit is: 11 +/- 1.02651 <INFO> HypoTestTool: expected limit (median) 11 <INFO> HypoTestTool: ... (13 Replies)
Discussion started by: Asif Siddique
13 Replies

5. Shell Programming and Scripting

Comparison treating strings as zero integers

I'm trying to write a bash script to perform basic arithmetic operations but I want to run a comparison on the arguments first to check that they're a number greater than zero. I want an error to pop up if the arguments args aren't >= 0 so I have: if ! ]; then echo "bad number: $1" fi ... (14 Replies)
Discussion started by: TierAngst
14 Replies

6. Shell Programming and Scripting

Cancel down 2 integers

Wonderful evening to all of you! My problem has to possible starting points. Well, not really, but getting to either one is no problem at all. So i got either a string in the format of "1920x1080" or simply the integers X = 1920 and Y = 1080. When I am done, I would like to have an output... (5 Replies)
Discussion started by: jakunar
5 Replies

7. Shell Programming and Scripting

integers, floats and text

I am using gawk in a dos shell in windows xp and want to read a datafile and reformat it. The datafile consists of columns of integers, floating point numbers and text strings. Each column is a fixed width and each column contains the same data type, eg all integers, all text. I am looking for a... (0 Replies)
Discussion started by: lookingfor help
0 Replies

8. Programming

Using write() with integers in C

I'm trying to write an integer to a file using the write() function, but write() requires the parameter to be written to be a const void*. How would I go about doing this? also: using itoa() produces a " warning: implicit declaration of function 'itoa' " even though i have #included stdlib.h (2 Replies)
Discussion started by: h@run
2 Replies

9. Shell Programming and Scripting

integers in the if statement

hi, im trying to compare two variables in csh to put in an if statement, eg: set a = $firstnum set b = $secondnum if ($a -ge $b) echo $a But I get an error ("if: Expression syntax"). How can I make csh see my variables as integers? thanks in advance! (5 Replies)
Discussion started by: Deanne
5 Replies

10. UNIX for Dummies Questions & Answers

how do you represent non integers in a shell script?

am trying to write a script that test that the load average which is taken from the uptime command's output to make sure it doesn't pass a certain limit. each time I execute the script, it complains about interger errors. if (2 Replies)
Discussion started by: TRUEST
2 Replies
Login or Register to Ask a Question