Count the field values in a file

Hi I have a file with contents like :

101,6789556897,0000795369 - seq[0152906817] - fmt_recs[187] - avg_recs[187] 
101,4678354769,0000835783 - seq[0000000397] - fmt_recs[98] - avg_recs[98] 
221,5679787008,0001344589 - seq[0000249687] - fmt_recs[1283] - avg_recs[1269]

I need to find the sum of the all the values (which are in bold).

here : my output should be sum of these values : 187,98,1283.

Can any one suggest me how to acheive this ?
:confused:

try..

 
awk -F"[][]" '{a+=$4}END{print "Sum is: "a}' filename

It didn't work :frowning:

% more text.dat
101,6789556897,0000795369 - seq[0152906817] - fmt_recs[187] - avg_recs[187] 
101,4678354769,0000835783 - seq[0000000397] - fmt_recs[98] - avg_recs[98] 
221,5679787008,0001344589 - seq[0000249687] - fmt_recs[1283] - avg_recs[1269]
% awk -F"[][]" '{a+=$4}END{print "Sum is: "a}' text.dat
Sum is: 0

Its working fine on AIX atleast.. what OS you working on?

 
/home/> awk -F"[][]" '{a+=$4}END{print "Sum is: "a}' v
Sum is: 1568
/home/> cat v
101,6789556897,0000795369 - seq[0152906817] - fmt_recs[187] - avg_recs[187]
101,4678354769,0000835783 - seq[0000000397] - fmt_recs[98] - avg_recs[98]
221,5679787008,0001344589 - seq[0000249687] - fmt_recs[1283] - avg_recs[1269]

Works for me

awk -F"[][]" '{a+=$4}END{print "Sum is: "a}' t
Sum is: 1568

What system are you trying this on?
What does this give you? awk -F"[][]" '{print $1"-"$2"-"$3"-"$4}'

It gave this output:

awk -F"[][]" '{print $1"-"$2"-"$3"-"$4}' text.dat
101,6789556897,0000795369 - seq-0152906817] - fmt_recs-187] - avg_recs-187] 
101,4678354769,0000835783 - seq-0000000397] - fmt_recs-98] - avg_recs-98] 
221,5679787008,0001344589 - seq-0000249687] - fmt_recs-1283] - avg_recs-1269]

I'm working on solaris

Seem that you have problem with filed separator.
Try this then

awk -F"\[|\]" '{a+=$4}END{print "Sum is: "a}' filename

or this

awk -F"[" '{a+=$3}END{print "Sum is: "a}' filename