Adding up columns of numbers...
root at trwatf.UUCP
root at trwatf.UUCP
Tue Jun 11 05:13:48 AEST 1985
>> P.S. One strange omission (to my knowledge) from all the UNIX tools
>> is a program, similar in command syntax (groan, but probably needed)
>> to 'sort' which adds up columns of numbers:
>>
>> sumcol +4.6 foo
>>
>> Strange, must have been written hundreds of times.
>
> Awk is great for this. Something like: awk '{sum += $4}\\
> END {print sum}'
> -mark
Yes indeedy... here's a quick little script to get you started with
multiple columns of numbers.....
#! /bin/sh
# Total multi-column numbers....
# TRW DSG EDS SISD SDL ATF STA-Jun-10
for f in $*
do
echo $f | awk '{ n = 80 - ( length($0) + 2 ) ; h = (n / 2) ; \
if ( n % 2 ) h = h-- ; \
for ( i = 1 ; i <= h ; i++ ) printf ">" ; \
printf " %s ",$0
for ( i = 1 ; i <= h ; i++ ) printf "<" ; \
printf "\n" \
}'
cat $f | \
awk 'BEGIN { \
FS = " " ;
for ( i = 0 ; i <= 25 ; i++ ) totals[i] = 0 \
} \
{ \
if (length($0) > 0 ) { \
print $0 ; k = 1 ; \
for ( i = 2 ; i <= NF ; i++ ) \
if ( length($i) > 0 ) { \
if ( substr($i,1,1) == "$" ) \
$i = substr($i,2,length($i) - 1)
k++ ; totals[k] += $i ; \
} \
} \
} \
END { printf "===================" ; \
for ( i = 2 ; i <= k ; i++ ) \
printf "================" ; \
printf "\n" ; \
printf "Total expenditures\t" ; \
for ( i = 2 ; i <= k ; i++ ) \
printf "\t\t$%d",totals[i] ; \
printf "\n" \
}'
done
--
UUCP: ...{decvax,ihnp4,allegra}!seismo!trwatf!root - Lord Frith
ARPA: trwatf!root at SEISMO
"Give a man a horse... and he thinks he's enormous"
More information about the Comp.unix.wizards
mailing list