8

I have a file as given below

--------------------------------------------------------------
Name_Customer   Item_Purchased  Item_Amount Credit
--------------------------------------------------------------
Tom              H1_P            7657        N/A    
Pras             Track_1         23          N/A
Cha              Brace           9           N/A
Moh              kite37          269         N/A
Prab             Bols            87699       N/A

I need to add the values under the column Item_Amount by ignoring the header in the file and print the sum as

Total Amount collected = 95657
11
awk '{s+=$3}END{print s}' yourfile
  • 1
    @coffeMug can you explain why use this: s+0?. – Cold Dec 15 '14 at 15:32
  • @Cold don’t remember exactly why that 0 is there! :-P Seems to be unnecessary so removed it! – coffeMug Dec 15 '14 at 19:43
  • @coffeMug probably to ensure awk treated the argument as a number – Thorbjørn Ravn Andersen May 9 '17 at 11:40
5

Pretty trivial using just awk. Assuming the example data is in a file, ex.txt:

$ awk '{total = total + int($3)}END{print "Total Amount collected = "total}' ex.txt

Example

$ awk '{total = total + $3}END{print "Total Amount collected = "total}' ex.txt 
Total Amount collected = 95657

Details

Using awk we collect the values from the 3rd column ($3) and accumulate their sub-total in the variable total. Once complete, as the last thing to do, END{..}, we print the message along with the value of the variable total.

  • just make one changes int($3) – Rahul Patil Oct 15 '13 at 6:45
  • 1
    That little int($3) gets things the way I need it. – bballdave025 yesterday
2
total=0; 
for n in  $( tail -n +4 /tmp/reports.txt | awk '{print $3}') ; 
do 
   total=$( expr $total + $n ); 
done ; 
echo ">>$total" 
  • 1
    You can't change the value of a variable inside a loop like that. See my answer here for a way to make variables accessible outside their loop.s – terdon Oct 14 '13 at 20:57
2

The awk approach is probably the easiest. Here are a few other choices:

Perl:

perl -lane '$k+=$F[2];END{print $k}' foo.txt

Pure coreutils:

t=0; tail -n +4 foo.txt | tr -s ' ' '\t' | cut -d $'\t' -f 3 | 
 while read i; do let t+=$i; echo $t; done  | tail -n 1
2

If it can help:

grep -Eo '[0-9\.]+' your_file|tr '\n' '+'|sed 's/\+$//'|bc -l
0

This pipeline should do the work:

tail -n +4 the_file | awk '{ sum += $3 } END { printf "Total Amount collected = %d\n", sum }'
-1
awk '{FS=","}{s+=$6}END{print s}' Filename
  • 1
    Can you explain it? – Prvt_Yadav Jan 22 at 13:19
  • Welcome to U&L, this hardly add anything to accepted answer and other, besides I don't see the use of FS. – Archemar Jan 22 at 13:37
  • Given that there are no commas in the sample input, how does this work? – Jeff Schaller Jan 22 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.