1834926 Members
2559 Online
110071 Solutions
New Discussion

Re: script question

 
SOLVED
Go to solution
Michael Treacy
Advisor

script question

I would like to be able to read a file with two columns and add all of the values in the second column, where the values in the first column are equal...

I started like this...
***
sort /usr/contrib/scripts/example >>$WORKPATH/$WORKFILE
cat $WORKPATH/$WORKFILE |while read SSN HOURS
do
let TOTAL=TOTAL+HOURS
echo $SSN $HOURS$TOTAL >> $WORKPATH/example.totals
done
***
I dont really need the total, just sub-totals by the first field - Any ideas are greatly appreciated.
5 REPLIES 5
RAC_1
Honored Contributor

Re: script question

total=$(awk '{s+=$2}' "your_file")

for i in $(cat "your_file"|awk '{print $1}')
do
if [[ ${i} = ${total} ]]
then
echo "$i = ${total}"
fi
done

Anil
There is no substitute to HARDWORK
Mel Burslan
Honored Contributor

Re: script question

for SSN in `cat $WORKFILE | awk {'print $1'} |uniq`
do
grep $SSN $WORKFILE > /tmp/sometempfile
tot=0
cat /tmp/sometempfile|while read SSN HOURS
do
let tot=$tot+$HOURS
done
echo $SSN" has "$tot" total hours"
done


hope this helps
________________________________
UNIX because I majored in cryptology...
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: script question

You don't bother to mention whether or not your first field is a string, integer, or floating point value nor do you bother to identify whether or not the values you are summing are simply integers. I'll assume the values to be summed are floating point; if not the fix is trivial. This is a good use of awk since you specified so little. Awk's arrays are associative which means that array["dog"] is just as valid as array[3].

Create an awk script, my.awk :
{
if ($1 in arry) arry[$1] += ($2 + 0)
else arry[$1] = ($2 + 0)
}
END {
for (i in arry)
{
printf("%-20.20s %9.2f\n",i,arry[i])
}
}
------------------------------------------

Now invoke it like this:
awk -f my.awk < myinfile | sort

I would create the awk script "on the fly" and include it in my shell script but this should get you started.

If the values to be summed are integers, change the %9.2f to %9d and similarly, you might want to change the %-20.20s format to %09d if the sunscripts are integer or %09.2f if the subscripts are floating point values. The format of the subscript only matters in the sorting of the output.

If it ain't broke, I can fix that.
Michael Treacy
Advisor

Re: script question

Beautiful, Thanks for reading what I didn't write
Michael Treacy
Advisor

Re: script question

THANKS