I have been trying to iterate over a file with 20 columns, using two ways, the first one is creating an array with the name of the columns and then passing it using for, but it does not work.
#!/bin/bash
a="${@}"
columns=('$col2' col3 col4 col5 col6 coll7 col8 col9 col10 col11 col12 col13 col14 col15 col16
col17 col18 col19 col20)
for elem in ${columns[*]}
do
while IFS=, read -r col1 col2 col3 col4 col5 col6 coll7 col8 col9 col10 col11 col12;do
b+=($elem)
done < $a
printf '%s\n' "${b[*]}"
done
The other method looks like to take the entire row, and that is not the idea, I would like to take the entire column individually, not by row. However this one did not work, looks like to be a problem with the way the for is written.
#!/bin/bash
a="${@}"
while IFS= read -r line; do
IFS=, read -ra fields <<<"$line"
for ((i=${fields[@]} ; i >= 1 ; i-- ))
do
printf '%s' "${fields[i]}"
done
done < $a
I have the next table, that represent some sales per year. I would like to take the information of each product per year and sum it, in order to verify the total, because in some cases this total values is incorrect. So for example in the year 2004, if you sum each product (45.000 + 70.000 + 100.000) the output is not 323.000 as the table is mentioned.