Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding $ amounts with expr with AIX 1

Status
Not open for further replies.

UCF87

Technical User
Feb 13, 2003
44
US
Is it possible in AIX to use the expr command to add a column of numbers? I have used the cut command to pull the amounts to an output file. For example my output file would show:

128.11
52.52
13.10
3512.09
etc..

Thanks in advance
 
echo $(awk -v ORS='+' '1' myOutputFile)0 | bc

NOTE: I bet you can do it all (column extraction and aggregation) ALL in awk, but that's a different thread/forum

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I will check with the awk forum, after executing that command it appears to be hung. After waiting for about 45 sec I did a control-C.
 
That worked Thanks!!... One more question

I will have negative numbers in this list as well for example the list will be:

125.11+
1234.45+
12.32-


Would I need to do 2 awk statements like:

awk '{if($0~/'-'/) print $0 }' input > output.negative
awk '{if($0~/'+'/) print $0 }' input > output.positive

Then perform your awk statement?

 
just curious, what happens when you do [assuming myOutputFile is the file with your extracted data as posted:

echo &quot;$(awk -v ORS='+' '1' myOutputFile)0&quot;

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I am still working on it. :cool:
For some reason I can't get a list of the positive numbers with my awk statement only negative.

This is what I have so far.

cut -c101-109 invout.pro.$1 > dls

awk '{if($0~/'-'/) print $0}' dls > dls.n
awk '{if($0~/&quot;'+'&quot;/) print $0}' dls > dls.p (<< this is not working for me)

awk 'BEGIN {SCNT=0}{SCNT+=$1}END{print SCNT}' dls.n > tdls.n
awk 'BEGIN {SCNT=0}{SCNT+=$1}END{print SCNT}' dls.p > tdls.p

Need something here like ((Total=tdls.p - tdls.n))

rm dls
rm dls.n
rm dls.p
 
leave the awk as it was originally posted and try it.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
With your original awk statement this is some of the output I get:

2167.33++ 86.32++ 912.85++ 197.78++ 1336.67++ 981.37++ 3965.29++ 358.78++
2879.50++ 130.17++ 870.66++ 23.15++ 29.10++ 692.92++ 778.44++ 585.27++
2104.19++ 862.60++ 503.91++ 204.66++ 70.64++ 3181.08++ 316.83++ 324.15++
2382.60++ 428.40++ 1790.16++ 79.46++ 427.29++ 4507.94++ 689.50++ 558.80++
1967.78++ 6.99++ 1565.77++ 1084.84++ 1473.70++ 51.69++ 26.96++ 1173.18++
822.00++ 2345.53++ 907.79++ 185.20++ 641.20++ 385.62++ 348.81++ 53.07++
 
ok, never mind my original post - I think it's a problem with the awk version on AIX. It works correctly on Solaris nawk and with the POSIX compliant awk on SOlaris as well.

I was refering to:

awk '{sum+=$1} END {print sum}' <file1

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I got want I needed. Thanks for your help... My final script:

cut -c101-109 invout.pro.$1 > dls

awk '{if($0~/'-'/) print $0}' dls > dls.n
awk '{if($0!~/'-'/) print $0}' dls > dls.p

awk 'BEGIN {SCNT=0}{SCNT+=$1}END{print SCNT}' dls.n > tdls.n
awk 'BEGIN {SCNT=0}{SCNT+=$1}END{print SCNT}' dls.p > tdls.p

neg=`cat tdls.n`
pos=`cat tdls.p`
TOTAL=`echo &quot;scale=4; $pos-$neg&quot; | bc`

echo $TOTAL

rm dls
rm dls.n
rm dls.p
 
If all of your input fields end with either '+' or '-' then...

awk '{e=substr($1,length($1)) $1;sum+=e}END{print sum}' <file1
 
Or, even better..

awk '/-$/ {sum-=$1}
!/-$/ {sum+=$1}
END {print sum}' <file1
 
Ygor... you humble me.
I was off by 2 cents with my script, and with your one line command I save time and I am right on the money!!

 
just curious, WHY do you need to distiguish positives from negatives? If you don't, the original awk script gives the correct aggregation [at least under Solaris' 'nawk'.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I have AIX 4.3.2, not sure I understand your question. If I don't distiguish positive numbers from negative I will not end up with a true amount.

With the original awk the amount shows 91119.4
with Ygor's awk the amount shows 87912.8 ( this is the correct amount)
 
strange - works just fine [withOUT distiguishing] under Solaris's awk-s. Doing '5+-2' and doind '5-2' results in '3'.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top