Skip to main content
3 of 4
added 4 characters in body
steeldriver
  • 83.8k
  • 12
  • 124
  • 175

Your attempt is close.

#!/bin/bash

sum=0
while IFS=, read -a fields; do
  if [[ ${fields[12]} = "$1" ]]; then
    (( sum += fields[5] ))
  fi
done < file.csv

printf 'sum: %d\n' "$sum"

You can shorten the if..then..fi using short-scircuit logic if you prefer ex.

[[ ${fields[12]} = "$1" ]] && (( sum += fields[5] ))

There's no need to skip the header line explicitly since it will fail the ${fields[12]} = "$1" test

steeldriver
  • 83.8k
  • 12
  • 124
  • 175