Calculate Row Percentage Bash One line Command -
i need know how calculate row percentages using 1 line bash command. suppose content of file test.tsv
looks this:
item sort clicks clicks b item 1 100 50 item 2 60 40
i want know percentage each click sort item like:
item sort clicks clicks b item 1 66.70% 33.33% item 2 60% 40%
any ideas on how this?
use awk kind of thing, bash doesn't real/floating-point arithmetic.
awk script:
nr==1 { print } nr>1 { printf("%s %d %.2f%% %.2f%%\n" , $1, $2 , ($3*100/($3+$4)) , ($4*100/($3+$4)) ) }
which means:
- if line number 1, print as-is.
- for other lines, print columns 1 , 2 as-is, calculate respective percentage columns 3 , 4.
in 1 line:
awk 'nr==1{print}nr>1{printf("%s %d %.2f%% %.2f%%\n", $1, $2, ($3*100/($3+$4)), ($4*100/($3+$4)))}' test.tsv
Comments
Post a Comment