subtotal columns

Hello

I have a file that has two (or more) different types of records I want to total. How would I do this using awk?

The file may contain several dozen records. The records are sorted on the database column - what I want to do is get the amount of space that each table has in that database and, add that amount to a variable. When the database changes, I want to print out that subtotal, reset the variable to zero and process the rest of the file.

I've seen examples where awk will total the ENTIRE column but, I need to create subtotals for each database when it changes (2,3,4,maybe more). I'm thinking awk would be the most effective way to go with this; is there something else that I could use? Any help would be greatly appreciated.

Thanks

Hi, post your source file.

Perl is very good at pulling data out of files and doing just about anything you want with it. But it all depends on your data and how it is formatted. Clarify your question and post the source file or some sample lines of the data. Anyone that wants to help will need that information regardless of what script/shell is used.

Sorry I didn't provide this earlier. Here is a sample of the file

Product Table1 300
Product Table2 226
Product Table3 410
Employee Table1 250
Employee Table2 300

So, I want to set up a do loop to test the first column (database). While the database is the same, I want to accumulate the values in column 3 (space). When the value of column 1 changes, I want to print the total amount of space in that database - so when the Product database changes, I want to print the value 936. After I print tat out, I want to zero the total variable out and continue with the Employee database

Here is the code I've written so far - it doesn't do what it's supposed to do but, it's a start

total=0
temp_db=" "
cat $file | while read line
do
db=`echo $line | cut -f1 -d" "`
difference=`echo $line | cut -f3 -d" "`
let total=$difference + $total
if [ "$db" != "$temp_db" ] && [ "$db" != "" ]; then
echo "space on $temp_db = $total
temp_db=$db
total=0
fi
done

nawk '
   {db[$1] += $3} 
END {
   for iter in db
      print iter, db[iter]
}' myFile

Thanks vgersh99 - your code works perfectly