how to sum each column in a file using bash

前端 未结 5 1857
梦谈多话
梦谈多话 2021-01-22 14:19

I have a file on the following format

id_1,1,0,2,3,lable1
id_2,3,2,2,1,lable1
id_3,5,1,7,6,lable1

and I want the summation of each column ( I

5条回答
  •  轮回少年
    2021-01-22 15:07

    Pure bash solution:

    #!/usr/bin/bash
    
    
    while IFS=, read -a arr
    do
            for((i=1;i<${#arr[*]}-1;i++))
            do
                    ((farr[$i]=${farr[$i]}+${arr[$i]}))
            done
            farr[$i]=${arr[$i]}
    done < file
    (IFS=,;echo "${farr[*]}")
    

提交回复
热议问题