title: sum up a column in text file - bash
body: <p>the text file</p>
  
  <p>student.txt</p>
  
  <pre><code>jane,good,3
  bob,bad,2.6
  annie,good,2.8
  dan,bad,2
  </code></pre>
  
  <p>first I tried to print all the good student which is successful</p>
  
  <pre><code>#!/bin/bash
  
  while IFS=, read -r -a array; do
     [[  "${array[1]}" == "good"  ]] || continue
     printf "%s \n" "${array[0]}" is a ${array[1]} student"
     done  &lt;  "student.txt"
  </code></pre>
  
  <p>output</p>
  
  <pre><code>jane is a good student
  annie is a good student
  </code></pre>
  
  <p>next I also want to sum up all the numbers of the third column in the text file after printing the types of student and it was not successful</p>
  
  <pre><code>#!/bin/bash
  while IFS=, read -r -a array; do
     [[  "${array[1]}" == "good"  ]] || continue
     printf "%s \n" "${array[0]}" is a ${array[1]} student"
  
     for n in "${array[2]}"; do
        total=$( "$total +=$n" | bc )
        echo $total
     done
  done  &lt;  "student.txt"
  </code></pre>
  
  <p>output</p>
  
  <pre><code>jane is a good student
  +=3: command not found
  annie is a good student
  +=2.8: command not found
  </code></pre>
  
  <p>expected output</p>
  
  <pre><code>jane is a good student
  annie is a good student
  total = 5.8
  </code></pre>
  
  <p>As I am new to bash scripting , I need to seek help from you guys.</p>
  
  <p>Oh another thing, I can't seem to make good use of awk utility.</p>
  
  <p>even if I tried a simple statement using awk<br>
  e.g</p>
  
  <pre><code>awk -F","  "{print $1}"  "student.txt"
  </code></pre>
  
  <p>by right if I am not wrong it should return something like this</p>
  
  <p>output </p>
  
  <pre><code>good
  bad
  good
  bad
  </code></pre>
  
  <p>but instead it returns me the whole value of the textfile which I have no idea why </p>
  
  <p>my output for awk -F","   "{print $1}"   "student.txt" </p>
  
  <pre><code>jane,good,3
  bob,bad,2.6
  annie,good,2.8
  dan,bad,2
  </code></pre>
  
  <p>so I guess any suggestion use awk method is out.</p>