Skip to main content
added obligatory "don't do this" warning
Source Link
steeldriver
  • 83.8k
  • 12
  • 124
  • 175

YourBefore considering doing it this way, please see Why is using a shell loop to process text considered bad practice?

Nevertheless, your attempt is close (one obvious issue is the whitespace around the = in your total_length assignment).

#!/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.

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

Before considering doing it this way, please see Why is using a shell loop to process text considered bad practice?

Nevertheless, your attempt is close (one obvious issue is the whitespace around the = in your total_length assignment).

#!/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.

added 4 characters in body
Source Link
steeldriver
  • 83.8k
  • 12
  • 124
  • 175

Your attempt is close.

#!/bin/bash

#!/bin/bash

sum=0
while IFS=, read -a fields; do
  if [[ ${fields[12]} = $1"$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"$1" ]] && (( sum += fields[5] ))

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

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

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

added 164 characters in body
Source Link
steeldriver
  • 83.8k
  • 12
  • 124
  • 175

Your attempt is close.

#!/bin/bash

#!/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

Your attempt is close.

#!/bin/bash

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

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

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

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

Source Link
steeldriver
  • 83.8k
  • 12
  • 124
  • 175
Loading