0

I have a scenario where i want to calculate the sum of below data set

V1|V2|V3  
"1.1"|"1.2"|"A"
"1.1"|"1.2"|"B"
"1.1"|"1.2"|"C"

sum of V1 V2 ?

how to that

output :

3.3 3.6 
3
  • 1
    Your question is unclear. Do you want to calculate the sums of the columns? (V1=3.3, V2=3.6) Or do you want a grand total? (V1+V2=6.9)
    – markgraf
    Jan 1 '20 at 17:31
  • sum of column i did like this not working : sed 's/"//g' | awk -F'|' '{ SUM += $1} END { printf "%.2f", SUM }' a.txt
    – genip26057
    Jan 1 '20 at 17:34
  • Does this answer your question? How to create a function to sum the required multiple column
    – markgraf
    Jan 1 '20 at 17:35
0
$ cat q
v1|v2|v3
"1.1"|"1.2"|"A"
"1.1"|"1.2"|"B"
"1.1"|"1.2"|"C"
$ sed 's|"||'g q | awk "-F|" \
    'BEG{v1=0;v2=0;}NR>1{v1 = v1 + $1; v2 = v2 + $2;}END{print v1 " " v2}'
3.3 3.6
3
  • its not an effective way to do bro need to parametric so that it can work with any file data like need to take column name V1 V2 V3 in parameter form to script incase in future the requirement may be like 5 column sum is needed then script should be able to calculate 5 column sum ... V1 V2 V3 V4 V5
    – genip26057
    Jan 2 '20 at 18:35
  • read my solution at the : unix.stackexchange.com/questions/559767/… ... scroll down till last you will find my solution
    – genip26057
    Jan 2 '20 at 18:37
  • gawk is not awk. Your solution is not portable. Besides, you need bash. Both answers from that thread do. On BSD, that's jut a pain, my answer still stands, while OP did not talk about dynamic columns.
    – SYN
    Jan 2 '20 at 20:07

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