Artificial intelligent assistant

filter by multiple columns in BASH I would like to filter one csv file without using grep or awk. In one variable I need the counter and in the other I need the sum of three columns. I've develop a script like this, but is not working. It seems that I can't filter using if conditions. Is there another way to filter columns without using grep neither awk? #!/bin/bash file=athletes.csv ((c=0)) ((sum=0)) for line in $(cat ${file}) do read id name nat sex date heigh weight sport gold silver bronze <<<${line} if (( nat == "$1" )) && (( sport == "$2" )) then ((c++)) #Participants ((sum+=gold+silver+bronze)) #Medals fi done echo "Count:$c, Sum:$sum"

First you should not read files like that, you could instead use a `while read` loop like:


while IFS=, read -r id name nat sex date heigh weight sport gold silver bronze; do
...
done < "$file"


Next you are using arithmetic expansion to compare what I'm assuming are strings which will not work, instead of `(( nat == "$1" ))` for example you probably want `[[ $nat == "$1" ]]`

Assuming your input file just contains only integer values in the gold, silver, and bronze columns you could use this instead:


#!/usr/bin/env bash

file=input.csv
c=0
sum=0

while IFS=, read -r id name nat sex date heigh weight sport gold silver bronze; do
if [[ $nat == "$1" && $sport == "$2" ]]; then
((c++))
((sum+=gold+silver+bronze))
fi
done < "$file"

printf 'Count: %d, Sum: %d\
' "$c" "$sum"

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy dc934947296094d95839a43898a5fb0b