I want to get the index number of a CSV column, depending on the column name.
For example I have these columns: TotalReported;Deceased;City
I tried doing this:
head -n1 data.csv | tr ";" "\n" | grep -nx "Deceased" | cut -d";" -f1
but nothing is being printed. I also tried doing this:
awk -F, '{ for(i=1; i<=NF; i++) if($i=="Deceased") {print i} exit}' data.csv'
which doesn't print anything. What can I do?
This is the content of the data.csv file:
Date_of_report;Municipality_code;Municipality_name;Province;Total_reported;Hospital_admission;Deceased
2020-03-13 10:00:00;GM0003;Appingedam;Groningen;0;0;0
2020-03-13 10:00:00;GM0010;Delfzijl;Groningen;0;0;0
2020-03-13 10:00:00;GM0014;Groningen;Groningen;3;0;0
2020-03-13 10:00:00;GM0024;Loppersum;Groningen;0;0;0
2020-03-13 10:00:00;GM0034;Almere;Flevoland;1;1;0
2020-03-13 10:00:00;GM0037;Stadskanaal;Groningen;0;0;0
2020-03-13 10:00:00;GM0047;Veendam;Groningen;0;0;0
2020-03-13 10:00:00;GM0050;Zeewolde;Flevoland;1;0;0
2020-03-13 10:00:00;GM0059;Achtkarspelen;Friesland;0;0;0
2020-03-13 10:00:00;GM0060;Ameland;Friesland;0;0;0
2020-03-13 10:00:00;GM0072;Harlingen;Friesland;0;0;0
2020-03-13 10:00:00;GM0074;Heerenveen;Friesland;0;0;0
Basically I want to get the index number of a column, and then put the index number in this awk command
v1=$(awk -F";" '{x+=$index}END{print x}' ./data.csv)
and get the sum of all the numbers of that specific column. The last three columns are important since you can only sum those of course.