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 
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
genip26057
  • 13
  • 5
  • 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 01 '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 01 '20 at 17:34
  • Does this answer your question? [How to create a function to sum the required multiple column](https://unix.stackexchange.com/questions/558377/how-to-create-a-function-to-sum-the-required-multiple-column) – markgraf Jan 01 '20 at 17:35

1 Answers1

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
SYN
  • 2,793
  • 12
  • 19
  • 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 02 '20 at 18:35
  • read my solution at the : https://unix.stackexchange.com/questions/559767/how-to-calculate-multiple-floating-point-column-sum ... scroll down till last you will find my solution – genip26057 Jan 02 '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 02 '20 at 20:07