I would like to calculate percentage of value in each line out of all lines and add it as another column. Input (delimiter is \\t):
1 10 2 10 3 20 4
You can do it in a couple of passes
#!/bin/bash total=$(awk '{total=total+$2}END{print total}' file) awk -v total=$total '{ printf ("%s\t%s\t%.2f\n", $1, $2, ($2/total)*100)}' file