-1

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
            
            
}
dsv2001
  • 1
  • 1
  • 5
  • 3
    Have a look at `awk` which allows you to process text files (`man awk`). Set the string separator to comma and then you can filter out the specific column and sum up the elements. – Tom Aug 26 '20 at 14:43
  • The [awk tutorial](http://grymoire.com/Unix/Awk.html) will give you a good grounding in how `awk` works. You'll want to focus on the `for` loop, passing in variables with `-v var="value"`, math ops, `sum+=$8` (for example), and logic comparisions, `if ($7 == var) { data_pos=7 }` Also will be good to understand how `BEGIN{}` and `END{}` blocks work. Good luck. – shellter Aug 26 '20 at 14:47
  • StackOverflow is a site for programmers. You are expected to show what you have tried when asking a question. See https://stackoverflow.com/help/how-to-ask for more. – jeremysprofile Aug 26 '20 at 15:46
  • Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). – Cyrus Aug 26 '20 at 15:49

1 Answers1

0

A simple awk approach:

$ cat input
foo;bar;baz
1;2;3
4;5;6
7;8;9
$ ./sum.sh foo < input
12
$ ./sum.sh qux < input
$ ./sum.sh baz < input
18
$ cat sum.sh 
#!/bin/sh

: ${FS=;}
col=${1:?}
awk 'NR==1 { for(i = 0; i <= NF; i++) if( $i == col ) c = i; next; }
        c { sum += $c }
        END{ if(c) print sum }
' col="$col" FS="$FS"
William Pursell
  • 204,365
  • 48
  • 270
  • 300