How to sum up two decimal values?

I am running the following script :

cat ind_sls_extr_UX.out_sorted | while read each_rec
do
  count=`echo "${each_rec}" | cut -c1-2`
  if [ ${count} -eq 45 ]
  then
  final_amount=0
       amount=`echo "${each_rec}" | cut -c280-287`
       echo "${amount}"
       final_amount=`expr ${amount} + ${amount}`
  fi
done

The value amount is fetching a decimal value, but in final amount the value is not getting summed up :

00454.74
expr: An integer value was expected.

00454.74
expr: An integer value was expected.

00454.74
expr: An integer value was expected.

00454.74
expr: An integer value was expected.

00454.74
expr: An integer value was expected.

00454.74
expr: An integer value was expected.

How to resolve it.

Pls search the forum before firing your question.

try using bc at the end

final_amount=`expr ${amount} + ${amount} |bc`

but, didn't get the expected result...now the result is summing up values upto one level only
Result :

00454.74
909.48
00454.74
909.48
00454.74
909.48
00454.74
909.48
00454.74
909.48
00454.74
909.48

[COLOR=Black]The file looks like...

[b]```text
45RA0290837AMERISOURCEBERGEN DRUG 9129364353+00046 09-24-101149372000U01000023360026224100262241 76225
2011 50458 58801 0914101128414+0000001 7474 S CAMINO DTUCSON AZ85746 00000
+00037024.84+00454.74+00149.66+00000305.08+00000305.08 PASCUA YAQUI TRIBE ARIZON20100924N1C

45RA0289062AMERISOURCEBERGEN DRUG 9129364356+00087 09-24-101148687000U010004340600157238AK782486300860
802 50458 58801 0914101127749+0000002 20940 BURBANK BWOODLAND HCA91367660100000
+00075924.20+00454.74+00386.53+00000136.42+00000136.42 KAISER FND HOSP PHARM 4352010092401C

60RA0290988AMERISOURCEBERGEN DRUG 9129364361+00057 09-24-101149373000U010002053200011438AK767668000860
802 50458 58801 0913101128415+0000002 10400 E ALAMEDADENVER CO80231 00000
+00032679.10+00454.74+00386.53+00000136.42+00000136.42 KAISER FDN HLTH PLN OF CO2010092401C

45RA0290736AMERISOURCEBERGEN DRUG 9129364366+00093 09-24-101149374000U010004665600344756BH611129176225
2011 50458 58801 0914101128416+0000001 6100 N HAGGERTYCANTON MI48187368300000
+00067329.69+00454.74+00149.66+00000305.08+00000305.08 HENRY FRD MEDCL CTR PHARM20100924Y1C

45RA0289000AMERISOURCEBERGEN DRUG 9129366455+00184 09-24-101148684000U01000614890000782000007820 76170
1481 50458 58801 0902101127747-0000012 47149 BUSE RD PATUXENT RMD20670154000000
+00237192.08+00454.74+00132.04-00003872.40-00003872.40 NAVAL MEDCL CLINIC 2010092405D

60RA0289000AMERISOURCEBERGEN DRUG 9129366455+00184 09-24-101148684000U010006148900055120AU467502776170
1481 50458 58801 0915101127747+0000006 73 NEALY BLVD LANGLEY AFVA23665202300000
+00237192.08+00454.74+00132.04+00001936.20+00001936.20 1 MDG SGSL 2010092401C

45RA0289000AMERISOURCEBERGEN DRUG 9129366455+00184 09-24-101148684000U010006148900110626BS700513476716
331 50458 58801 0915101127747+0000003 6501 N CHARLES TOWSON MD21204681900000
+00237192.08+00454.74+00454.51+00000000.69+00000000.69 SHEPPARD PRATT HLTH SYS 2010092401C

45RA0316958AMERISOURCEBERGEN DRUG 9129371835+00372 09-24-101148692000U010041354800008136BD224763676170
1481 50458 58801 0916101127758+0000048 36000 DARNALL LFORT HOOD TX76544 00000
+00247836.02+00454.74+00132.04+00015489.60+00015489.60 DARNALL ARMY COMM HOSP 2010092401C

60RA0316958AMERISOURCEBERGEN DRUG 9129371835+00372 09-24-101148692000U010041354800008149AW533330376170
1481 50458 58801 0916101127758+0000007 2200 BERGQUIST SAN ANTONITX78236990700000
+00247836.02+00454.74+00132.04+00002258.90+00002258.90 WHMC/MSLS (FM3047) 2010092401C

60RA0316958AMERISOURCEBERGEN DRUG 9129371835+00372 09-24-101148692000U010041354800248892BM572513876225
2011 50458 58801 0916101127758+0000001 400 W 4TH ST ODESSA TX79761504500000
+00247836.02+00454.74+00149.66+00000305.08+00000305.08 MEDICAL CTR HOSP OUTPATIE20100924Y1C

Looks like a little logic error to me.
The variable ${final_amount} is being zeroised on every iteration.
We should move to to outside the loop.
The arithmetic line is faulty in logic (adding the wrong variable) and syntax (expr can't deal with decimal places). You presumably want to add ${amount} to ${final_amount} on each iteration.
Also, without knowing what Shell you are using it is hard to be sure whether the updated variable is available outside the loop.

final_amount=0
cat ind_sls_extr_UX.out_sorted | while read each_rec
do
  count=`echo "${each_rec}" | cut -c1-2`
  if [ ${count} -eq 45 ]
  then
       amount=`echo "${each_rec}" | cut -c280-287`
       echo "${amount}"
       final_amount=`echo "${final_amount} + ${amount}"|bc`
  fi
done

changed code :

while read each_rec
do
  count=`echo "${each_rec}" | cut -c1-2`
  if [ ${count} -eq 45 ]
  then
       amount=`echo "${each_rec}" | cut -c280-287`
       echo "${amount}"
       final_amount=`echo "${amount} + ${amount}" | bc`
echo  ${final_amount}
else
echo "next"
fi
done < ind_sls_extr_UX.out_sorted

result getting :

00454.74
909.48
00454.74
909.48
00454.74
909.48
00454.74
909.48
00454.74
909.48
00454.74
909.48
next
next

Sorry, you may have been too quick while I was editing and re-editing the post. I realised that michaelroxar17's correction didn't actually work and went off to test an alternative (which is in the corrected post).

Note that we are incrementing the running ${final_amount} rather than adding ${amount} to ${amount} which just doubled the value!

final_amount=`echo "${final_amount} + ${amount}"|bc`
1 Like

It worked..actually I've seen ur post after I implemented the same....thanks a lot....:b::b::b:

awk 'BEGIN{RS=""}/^45/{amount=substr($0,280,8);sum+=amount;print amount, sum}' infile

00454.74 454.74
00454.74 909.48
00454.74 1364.22
00454.74 1818.96
00454.74 2273.7
00454.74 2728.44