Calculate percent using values in 2 files

Trying to use file1 which is the actual counts in $2 associated with each $1 entry. The total of each $1 is in file2 with the total in $3 . So when there is a match between $1 in file1 with $1 in file2 , then the % is calculated using the $2 value of file1 and $3 value of file2 . Thank you :).

file1

AAA 1000
BBB 0
CCC 400
DDD 0
EEE 50

file2

AAA 2 1000
BBB 5 200
CCC 1 400
DDD 2 800
EEE 3 500

desired output

AAA 100.0%
BBB 0.0%
CCC 100.0%
DDD 0.0%
EEE 10.0% 

awk with current output

awk 'FNR==NR{A[$1]=$2;next} ($1 in A){X=(A[$1]/$3)*100;printf("%s %.2f\n",$1,  100-X)}' file1 file2
AAA 0.00
BBB 100.00
CCC 0.00
DDD 100.00
EEE 90.00

change 100-X to X and you're done.

Or use this(if both files are sorted by the key field):

join file1 file2 | awk '{ printf "%3s %6.2f %\n",$1,$2/$4*100 }'

if the files are unsorted, you have to sort them first:

sort file1 -o file1
sort file2 | join file1 - | awk '{ printf "%3s %6.2f %\n",$1,$2/$4*100 }'
1 Like

A little modification to yours will do:

awk 'FNR==NR{A[$1]=$2;next} ($1 in A){X=(A[$1]/$3)*100;printf("%s %.1f%\n",$1, X)}' file1 file2

Or even this:

awk 'FNR==NR{A[$1]=$2;next} $1 in A{printf "%s %.1f%\n",$1, A[$1]/$3*100}' file1 file2
1 Like

Thank you very much :).

The standard way to print a percent sign character in a printf format string (in C, in the printf utility, and in awk ) is with %% . Some versions of awk will let you get by with:

awk 'FNR==NR{A[$1]=$2;next} $1 in A{printf "%s %.1f%\n",$1, A[$1]/$3*100}' file1 file2

Others will give you a diagnostic similar to:

awk: weird printf conversion %

 input record number 1, file2 
 source line number 1
awk: not enough args in printf(%.1f%
)
 input record number 1, file2 
 source line number 1
awk 'FNR==NR{A[$1]=$2;next} $1 in A{printf "%s %.1f%%\n",$1, A[$1]/$3*100}' file1 file2

should work with any awk .

1 Like