basically for example if you have a csv file with a header called "Totalreported". If I type in the command line "Totalreported" it should output the sum of all the values of that specific column. And if for example I have a different csv, should be the same process. And if I misspell a column name or the column name doesnt exist it wont output anything. How can I do that? So no hard-encoded column names in the script.
Here is what I have tried but in here are hard-encoded values, but I want it to be not hard-encoded, so that I can use any csv file. Here you can see the hard encoded column names "Deceased", "Hospital", and "TotalReported", but I want my code to be able to take in any column from any csv file depending on the input How can I achieve this?
#!/bin/bash
updatedata() {
if [ $pos -eq 0 ]
then
if [ "$1" = "Deceased" ]
then
v0=$(awk -F";" '{x+=$7}END{print x}' ./COVID-19_aantallen_gemeente_cumulatief.csv )
elif [ "$1" = "Hospital" ]
then
v0=$(awk -F";" '{x+=$6}END{print x}' ./COVID-19_aantallen_gemeente_cumulatief.csv)
elif [ "$1" = "TotalReported" ]
then
v0=$(awk -F";" '{x+=$5}END{print x}' ./COVID-19_aantallen_gemeente_cumulatief.csv)
fi
elif [ $pos -eq 1 ]
then
if [ "$1" = "Deceased" ]
then
v1=$(awk -F";" '{x+=$7}END{print x}' ./COVID-19_aantallen_gemeente_cumulatief.csv)
elif [ "$1" = "Hospital" ]
then
v1=$(awk -F";" '{x+=$6}END{print x}' ./COVID-19_aantallen_gemeente_cumulatief.csv)
elif [ "$1" = "TotalReported" ]
then
v1=$(awk -F";" '{x+=$5}END{print x}' ./COVID-19_aantallen_gemeente_cumulatief.csv)
fi
}