Math calculation over shell

Hi

I am trying to calculate the rate at which something is happening.
I have 2 files- a1 and b1.

I want to calculate something like this

((wc -l a1)/(wc -l a1 + wc -l b1))*100 

over a loop for different a and b.
Is this possible, help me out fellas.

Thanks a lot :slight_smile:

It'd probably make more sense to calculate the number of lines for a file once instead of repeatedly -- far less work.

This should work in bash or ksh:

#!/bin/bash

# Feeds lines into wc -l as arguments.  A file containing lines 'a', 'b', 'c' would
# run wc -l a b c to count lines for all those files.
xargs wc -l < listofa > linesa
xargs wc -l < listofb > linesb

while read LINEA FILEA
do
        [ "$FILEA" = "total" ] && continue # Skip 'total' lines

        while read LINEB FILEB
        do
                [ "$FILEB" = "total" ] && continue # Skip 'total' lines

                echo $FILEA $FILEB $(( (LINESA*100) / (LINESA+LINESB) ))
        done <linesb
done <linesa

rm -f linesa linesb
1 Like

Certainly that makes a lot more sense than what I was thinking :slight_smile:
Thanks a lot Corona688

And Corona688's formula does the multiply before the divide which is so much more accurate in integer arithmetic.

hi, a followup question-
Corona688's solution loops every value in listofa for all values in listofb, making n*n iterations. How do I make is to run for just one loop each- i.e if listof a has

file1
file2
file3

and listofb has

file11
file22
file33

. I want it to run just

file1 file11
file2 file22
file3 file33

.

Thanks again!

Found a simple workaround with awk.

THanks guys!