Math calculation over shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Math calculation over shell
# 1  
Old 05-15-2012
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

Code:
((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 Smilie
# 2  
Old 05-15-2012
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:
Code:
#!/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

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-15-2012
Certainly that makes a lot more sense than what I was thinking Smilie
Thanks a lot Corona688
# 4  
Old 05-15-2012
And Corona688's formula does the multiply before the divide which is so much more accurate in integer arithmetic.
# 5  
Old 05-18-2012
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
Code:
file1
file2
file3

and listofb has
Code:
file11
file22
file33

. I want it to run just
Code:
file1 file11
file2 file22
file3 file33

.

Thanks again!
# 6  
Old 05-21-2012
Found a simple workaround with awk.

THanks guys!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script for solving the math question

Can such Puzzle solve through UNIX script? if yes, what could be the code? This has been solve in C language. we were trying to solve this through shell but could not because of not able to pass 1st argument with multiple value. we are not expert in unix scripting. Below is the puzzle John is a... (4 Replies)
Discussion started by: anshu ranjan
4 Replies

2. Shell Programming and Scripting

Numeric calculation in shell scripting

Hi Team, how can i calculate the number as below in shell script for below expression. 34 /50 * 100 equals to 68% now how i would represent this in shell script. Thanks, Jewel (23 Replies)
Discussion started by: Jewel
23 Replies

3. UNIX for Dummies Questions & Answers

Variables and math in Old skool Bourne Shell

Hey everybody, I've been searching google and these forums and have found some solutions to the issues I've been having today within the OLD Bourne Shell. I am following chapter 6 of the Guide to Unix using Linux 4th Edition. I am working on some basic calculations with variables in the BASH... (3 Replies)
Discussion started by: mr.rhtuner
3 Replies

4. Shell Programming and Scripting

math calculation for a txt file

Hi All, I have a text file which is only one column. I want to multiply all these values in file1.txt by 0.01 and get the output.txt file1.txt 65 85 90 ... output.txt 0.65 0.85 0.90 ... Thanks. Please use code tags when posting data and code samples! (2 Replies)
Discussion started by: senayasma
2 Replies

5. Shell Programming and Scripting

A Math problem using shell script

Have a bit complicated math query .. Basically i am given a number which is > 50 .. I am suppose to find the calculation to get a number which is equal or more than the input number and is also a multiple of any number between 20 - 30 . For example . Input number is 60 . Now 20x3 =60 ... (2 Replies)
Discussion started by: greycells
2 Replies

6. Shell Programming and Scripting

Modulo calculation in shell script

hi i am writing a progrm to print the even numbers and the code which i am following is as follows #!/usr/bin/bash echo "enter a number a" read a if then echo "the number is even" else echo "the number is odd" fi ~ what is the mistake i am doing ...please tell me (3 Replies)
Discussion started by: harjinder
3 Replies

7. Shell Programming and Scripting

calculation using awk or shell script in between the numbers

file A E969K D223L E400L E34L file B predicted 3 1 250 251 500 501 1000 The output should be E969K 501 1000 D223L 1 250 E400L 251 500 E34L 1 250 I tried in this way (1 Reply)
Discussion started by: cdfd123
1 Replies

8. Shell Programming and Scripting

Script math calculation

Hi Gurus, I'm currently using HP-UX B.11.23. I've a simple calculation script which performs the task below. -> echo "240021344 / 1024 /1024" | bc Output: 228 240021344 is KB value. When I tried to perform the same calculate in Ms Excel, it produces a different result: 228.9021912.... (12 Replies)
Discussion started by: superHonda123
12 Replies

9. UNIX for Dummies Questions & Answers

Can Any help me with the math on this shell script?

Develop a grade calculating program. This program will process all students in the file. This program should neatly display each field of each student's record *and* adds the following items: Course Average and Letter Grade. The course average is calculated by the following weights: 50% for quiz... (7 Replies)
Discussion started by: knp808
7 Replies

10. Shell Programming and Scripting

Math calculation help

Hi, I wrote this script awk -F"\t" '{if ((($1 == 586) || ($1 == 68030)) && (($2/1024) < 512)) print $0"\t"(512-($2/1024))"\t"(512-($2/1024))/256}' pcs.txt But I want from the calculation in red to get rid of the decimal part. Like instead of 1.75 to keep only 1.Please somebody tell me what... (4 Replies)
Discussion started by: sickboy
4 Replies
Login or Register to Ask a Question