0

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.

enter image description here

https://easyupload.io/3lpz6p

Andres
  • 59
  • 2
  • 11

1 Answers1

1

While Bash may not be the most efficient way to verify the total of each column, there is a way to do it and I had fun finding the solution.

Background

I have found a way to obtain the total of the values in one column using Bash. My idea was to use the read command and make sure to only read the values in the column specified by the user in the variable col_num. This is because the read command goes through a file row by row. In the example below, I specified col_num to be 0, which means the read command will go through the .csv input line by line, while grabbing only the values in the first column. I then did addition of the values the Bash way according to this.

I believe that only a small adjustment to my code is needed to enable it to go through all 20 columns before it terminates. But since you only have 20 columns, I thought it would not be too bad to increment col_num for every column.

My solution

#!/bin/bash

{
    # this reads the first row which has the column names so we will not go 
    # through that row in the loop below
    read

    # this is where you specify the column number
    col_num=0

    # var_2 specifies the number of values (length) grabbed per line. we only 
    # want one value from each line so let it be 1
    var2=1

    while IFS=, read -a arr
    do
        final_arr+=`echo "${arr[@]:$col_num:$var2} + "`
        column_total="$(($final_arr 0)) "      
    done
    echo $column_total
} < input.csv

Important note: The script works for every column except for the last column, probably because it does not end with a comma.

fxdeaway
  • 165
  • 8