Getting Sum, Count and Distinct Count of a file

Hi all this is a UNIX question.

I have a large flat file with millions of records.
col1|col2|col3
1|a|b
2|c|d
3|e|f
3|g|h
footer****

I am supposed to calculate the sum of col1 1+2+3+3=9, count of col1 1,2,3,3=4, and distinct count of col1 1,2,3=c3

I would like it if you avoid external commands like AWK. Also, can we do the same by creating a function?

Please bear in mind that the file is huge

Thanks in advance

Is there any reason to avoid external commands? Is this a homework question?

Regards

Hi i can solve your first 2 requirement the third i hope some one will post the solution ... will the solution for first 2 options follows
------------------------------
NumOfColumn=0
SumOfColumn=0
while read line
do
var1=`echo $line | cut -d"|" -f1`
SumOfColumn=`expr $SumOfColumn + $var1`
NumOfColumn=`expr $NumOfColumn + 1`
done < larg_file.txt

echo -e "SumOfColumn=$SumOfColumn\nNumOfColumn=$NumOfColumn"
#This will work :b:
---------------------------------

for third option you have to read sort or uniq command in book

nawk 'BEGIN{FS="|"}
{
split($0,arr,"|")
sum+=arr[1]
n++
_[arr[1]]=1
}
END{
for(i in _)
 m++
print "Sum:"sum
print "Cnt:"n
print "Dis:"m
}' file

Are you people stupid? The forum moderator asked if this was a homework question but then you go and post the solution. I think I answered my own question.