HI guys,

I have file with 6 values in:

1060276
2211546
7544.941158316
1060276
2211606
7545.960677177

I put those numbers into variables:

obyte1=`cat $TMP | sed -n 1p`
  rbyte1=`cat $TMP | sed -n 2p`
  snapt1=`cat $TMP | sed -n 3p`
  obyte2=`cat $TMP | sed -n 4p`
  rbyte2=`cat $TMP | sed -n 5p`
  snapt2=`cat $TMP | sed -n 6p`

I know need to perform the following calculation:

read=((obyte2 - obyte1) / (snapt2 - snapt1)) * 1000000

The question is how do i do this im scripting using ksh (cant be changed) but need the answer to be accurate to multiple decimal places and also do subtraction with numbers to multiple decimal places. Can anyone help??

Variables in the shell are strings. Another utility is necessary to make the evaluation.
expr can do it, but has many "gotchas" that you need to be aware of, e.i. spaces, (), *, which need to be stripped of its especial meaning.

result=$(expr  \( \( $obyte2 - $obyte1 \) / \( $snapt2 -  $snapt1 \) \) \* 1000000)

That gives me the following error:

expr: non-numeric argument
expr: non-numeric argument

So you can try it for yourself i have attached the exact code im using:
temp file=/tmp/net_vmxnet0

1977014
4458867
13127.941239176
0
1977064
4459323
13128.983542666

And the code that process that data is

TMP=/tmp/net_vmxnet0
  echo "$data" > $TMP
  obyte1=`cat $TMP | sed -n 1p`
  rbyte1=`cat $TMP | sed -n 2p`
  snapt1=`cat $TMP | sed -n 3p`
  obyte2=`cat $TMP | sed -n 5p`
  rbyte2=`cat $TMP | sed -n 6p`
  snapt2=`cat $TMP | sed -n 7p`
  ifspee=`cat $TMP | sed -n 8p`
  read=$(expr  \( \( $obyte2 - $obyte1 \) / \( $snapt2 -  $snapt1 \) \) \* 1000000)
  echo $read

>That gives me the following error
Yeap, expr doesn't support floating points
In bash you'll need to use bc, calc, awk, or perl
But ksh-93 supports floating points

result=$(((($obyte2 - $obyte1) / ($snapt2 - $snapt1)) * 1000000))

Hi,

I understand ksh-93 does support float but it is not an option to use this. How would I do it with bc, calc, awk or perl?

I understand ksh-93 does support float but it is not an option to use this. How would I do it with bc, calc, awk or perl?

result=$(echo $var1 $var2 | awk '{print $1 - $2}')

$1 inside awk becomes whatever variable you past first, and $2 becomes the second variable and so on.
More about awk
bc examples

commented: thanks the help much apreciated! +3

HI guys,

I have file with 6 values in:

1060276
2211546
7544.941158316
1060276
2211606
7545.960677177

I put those numbers into variables:

obyte1=`cat $TMP | sed -n 1p`
  rbyte1=`cat $TMP | sed -n 2p`
  snapt1=`cat $TMP | sed -n 3p`
  obyte2=`cat $TMP | sed -n 4p`
  rbyte2=`cat $TMP | sed -n 5p`
  snapt2=`cat $TMP | sed -n 6p`


Calling sed six times???? AARRGGHH!!!

If you know there are going to be six lines (or only need the first six):

{
  read obyte1
  read rbyte1
  read snapt1
  read obyte2
  read rbyte2
  read snapt2
} < "$TMP"


I know need to perform the following calculation:

read=((obyte2 - obyte1) / (snapt2 - snapt1)) * 1000000


Or just use awk:

awk '{ n[NR] = $0 }
 END { print ( n[4] - n[1] ) / ( n[6] - n[3] ) * 1000000 }
' "$TMP"

HI,

Got the code working but the results look a little wierd heres the code:

kstat -p -c net -n $Interface 1 3 | egrep\
  'snap|rbytes64|obytes64|ifspeed' |tail -7 | awk '{print $2,$3}' > $TMP
  #write
  awk '{ n[NR] = $0 }
  END { print ( n[5] - n[1] ) / ( n[7] - n[3] ) * 1000000 }
  ' "$TMP"
  #read
  awk '{ n[NR] = $0 }
  END { print ( n[6] - n[2] ) / ( n[7] - n[3] ) * 1000000 }

in tmp is the following:

19229032
66688301
522085.331618869
1000000000
20086366
66711579
522086.341435589

And the results are:

8.49e+11
2.30517e+10

What does the e+11 and e+10 mean??

>What does the e+11 and e+10 mean??
Search for scientific notation. When a number is large in length (very big or very small), a method has been invented to represent it using exponent.

Thought so just wanted to make sure it wasnt an error code from awk!! ta very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.