0

I have got a .csv files, with some numbers, I want to get a new file with the same number of rows and one column, with the sum of the numbers of the corresponding row in the original .csv file.

Input example (note the whitespaces, I hope this would not be a problem)

 2,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
 3,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
 4,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
 5,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
 6,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0

Expected output

2
4
5
6
7

My code (not producing any output):

    file_out="output.txt"
    file_in="input.txt"
    cmd3="sum_file=\$(awk -F, '
    {
        sum = 0; 
        for (i=1; i<=NF; i++) {
            sum += \$i;
        }
        print sum;
    }
    ' ${file_in})"
    echo $cmd3
    eval $cmd3
    eval "echo ${sum_file} > ${file_out}"

You can propose also tools different from awk, but I would be glad to know why my code does not work, maybe because of the whitespaces (I do not think so).

  • Why are you storing your code in a variable and then evaling it? That seems very convoluted. Are there any requirements you're not telling us? – glenn jackman Feb 1 '18 at 20:15
  • Wait, what output are you expecting? To the screen? You are redirecting to the output file. Is that file empty? – glenn jackman Feb 1 '18 at 20:16
  • @glennjackman, the problem was in the last line. I should have used \${sum_file}. I was using eval for debug purposes, since it was easy to echo the lines. But doing so I introduced a new bug :( At least now is solved – Nisba Feb 1 '18 at 22:43
2

You don't need any temporary values. Just execute awk and redirect output to file.

file_out="output.txt"
file_in="input.txt"
awk -F, '
{
    sum = 0; 
    for (i=1; i<=NF; i++) {
        sum += \$i;
    }
    print sum;
}
' "${file_in}" > "${file_out}"
  • 1
    You don't really need to have the file names in variables, either. If you do, they should be inside double quotes. (It seems to work fine as long as your file names are trivial, but when it breaks, we get another one in the landfill of desperate newbie questions about why this doesn't work on file names with asterisks, spaces, square brackets etc in their names.) – tripleee Feb 28 '18 at 12:34
  • Good point, @tripleee – Tomasz Jakub Rup Feb 28 '18 at 14:01
  • sum += \$i; causes a syntax error unless it's part of e.g. cmd3="...". – simlev Apr 17 '18 at 20:11
1

awk:

awk -F, '{sum=0; for (i=1; i<=NF; i++) {sum+= $i} print sum}' input.txt > output.txt

perl:

perl -F, -lane '$s=0; $s+=$_ for @F; print $s' input.txt > output.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.