Compare and calculate two variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare and calculate two variables
# 1  
Old 11-27-2013
Compare and calculate two variables

Hi, I have two variables with some values with 5 decimal digits and separated with space, the values of the variables are different, I want to make operations with this two variables.
The way was I find to do it itīs with files and
Code:
paste file1 file2 | awk '{print $1 - $2}'

I want to make it with the variables without files.
There is a way to do it?

Code:
Variable1=89.90000 21.65000 28.10000 29.20000 1.80000 2.80000 8.30000 9.80000 1.59770 1.65250 0.91650 0.90270 102.22000 100.80000 1.34410 1.36110 11.91670 7.46430 2.58430 2.13270 4515.47000 4322.26000 2.73080 2.73080 16072.54000 1220.55000 1220.55000
Variable2=95.90000 61.65000 28.10000 29.20000 1.80000 2.80000 8.30000 9.80000 1.59770 5.65250 0.91650 0.90270 102.22000 100.80000 1.34410 1.34110 11.91470 7.46430 2.58430 2.13270 4515.47000 4322.26000 2.73080 2.73080 16072.54000 1220.55000 1220.55000


Last edited by radoulov; 11-27-2013 at 12:43 PM..
# 2  
Old 11-27-2013
Assign these variables to awk variables, split them and store them in arrays and perform whatever operation that you want:

Here is an example:
Code:
Variable1="89.90000 21.65000 28.10000 29.20000 1.80000 2.80000 8.30000 9.80000 1.59770 1.65250 0.91650 0.90270 102.22000 100.80000 1.34410 1.36110 11.91670 7.46430 2.58430 2.13270 4515.47000 4322.26000 2.73080 2.73080 16072.54000 1220.55000 1220.55000"
Variable2="95.90000 61.65000 28.10000 29.20000 1.80000 2.80000 8.30000 9.80000 1.59770 5.65250 0.91650 0.90270 102.22000 100.80000 1.34410 1.34110 11.91470 7.46430 2.58430 2.13270 4515.47000 4322.26000 2.73080 2.73080 16072.54000 1220.55000 1220.55000"

awk -v V1="$Variable1" -v V2="$Variable2" '
        BEGIN {
                split ( V1, A1 )
                split ( V2, A2 )
                printf "%.5f %.5f %.5f\n", A2[1], A1[1], A2[1] - A1[1]
        }
'

# 3  
Old 11-27-2013
Quote:
Originally Posted by Yoda
Assign these variables to awk variables, split them and store them in arrays and perform whatever operation that you want:

Here is an example:
Code:
Variable1="89.90000 21.65000 28.10000 29.20000 1.80000 2.80000 8.30000 9.80000 1.59770 1.65250 0.91650 0.90270 102.22000 100.80000 1.34410 1.36110 11.91670 7.46430 2.58430 2.13270 4515.47000 4322.26000 2.73080 2.73080 16072.54000 1220.55000 1220.55000"
Variable2="95.90000 61.65000 28.10000 29.20000 1.80000 2.80000 8.30000 9.80000 1.59770 5.65250 0.91650 0.90270 102.22000 100.80000 1.34410 1.34110 11.91470 7.46430 2.58430 2.13270 4515.47000 4322.26000 2.73080 2.73080 16072.54000 1220.55000 1220.55000"

awk -v V1="$Variable1" -v V2="$Variable2" '
        BEGIN {
                split ( V1, A1 )
                split ( V2, A2 )
                printf "%.5f %.5f %.5f\n", A2[1], A1[1], A2[1] - A1[1]
        }
'

Hi Yoda, thanks for your reply, can you explain your solution? I have maked this and it show only 3 values, the operation of - itīs between variable 1-variable 2?
thanks again.
# 4  
Old 11-27-2013
Assigning your shell variables Variable1 & Variable2 to awk variables V1 & V2:
Code:
awk -v V1="$Variable1" -v V2="$Variable2"

Splitting each elements in V1 & V2 separated by blank spaces and storing them to an array A1 & A1 (starting index = 1):
Code:
                split ( V1, A1 )
                split ( V2, A2 )

Now you can reference any element in your array and do required operation.
# 5  
Old 11-27-2013
Quote:
Originally Posted by Yoda
Assigning your shell variables Variable1 & Variable2 to awk variables V1 & V2:
Code:
awk -v V1="$Variable1" -v V2="$Variable2"

Splitting each elements in V1 & V2 separated by blank spaces and storing them to an array A1 & A1 (starting index = 1):
Code:
                split ( V1, A1 )
                split ( V2, A2 )

Now you can reference any element in your array and do required operation.
Yoda Im testing the solution and it show the first value of both variables and 0.00000 in the third value.
Can you put the code of one example with the values of the variables that I haved pasted with the operation - ?
I need the resulting these same values but with the arhitmetic operation beetween.

Thanks
# 6  
Old 11-27-2013
Why don't you explain what you are trying to do and also show us your expected output?

This is what I get running the code that I posted and using your variable values:
Code:
95.90000 89.90000 6.00000

So A2[1] - A1[1] means, subtracting the first element of Variable2 with first element Variable1
# 7  
Old 11-27-2013
Quote:
Originally Posted by Yoda
Why don't you explain what you are trying to do and also show us your expected output?

This is what I get running the code that I posted and using your variable values:
Code:
95.90000 89.90000 6.00000

So A2[1] - A1[1] means, subtracting the first element of Variable2 with first element Variable1
Ok I got it but the code its so long and if i have a large collection (1000) I need to write number by number of the index value to do the operation, some way more simple?

Code:
awk -v V1="$VAR1" -v V2="$VAR2" '
BEGIN {
        split ( V1, A1 )
        split ( V2, A2 )
        printf "%.5f %.5f %.5f %.5f %.5f %.5f %.5f %.5f %.5f %.5f  %.5f  %.5f  %.5f  %.5f %.5f %.5f %.5f %.5f  %.5f %.5f %.5f %.5f %.5f %.5f %.5f %.5f %.5f", A1[1] - A2[1], A1[2] -  A2[2], A1[3] - A2[3],A1[4] - A2[4],A1[5] - A2[5],A1[6] - A2[6], A1[7] - A2[7], A1[8] - A2[8], A1[9] - A2[9], A1[10] - A2[10], A1[11] - A2[11], A1[12] - A2[12], A1[13] - A2[13], A1[14] - A2[14], A1[15] - A2[15], A1[16] - A2[16], A1[17] - A2[17], A1[18] - A2[18], A1[19] - A2[19], A1[20] - A2[20], A1[21] - A2[21], A1[22] - A2[22], A1[23] - A2[23], A1[24] - A2[24], A1[25] - A2[25], A1[26] - A2[26], A1[27] - A2[27]
        }
'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two variables and print the difference

compare two variables and print the difference i have two variables X1=rac1,rac2 Y1=rac2,rac3 output=rac1,rac3 Use code tags to wrap code fragments or data samples. (1 Reply)
Discussion started by: jhonnyrip
1 Replies

2. Shell Programming and Scripting

Compare two variables and print the difference

Hi PRIM_SEQ=`some sql code` and output of PRIM_SEQ is like below 120 130 STB_SEQ=`some sql code` and output of STB_SEQ is like below 115 110 i need to compare this two variables output ( decimal numbers) 1) What I want to do is to compare every number in the PRIM_SEQ with... (8 Replies)
Discussion started by: amar1208
8 Replies

3. Shell Programming and Scripting

read line by line and calculate the co-presence of variables

Hey guyz, I have a table which shows the presence or absence of my variables (A,B,C,...) in my observations (1,2,3,...) * A B C ... 1 1 0 1 2 1 1 0 3 1 0 0 ... I want to calculate the co-presence of my variables. to have a table shows the pairwise presence of the variables (have... (1 Reply)
Discussion started by: @man
1 Replies

4. Shell Programming and Scripting

How to compare floating variables , integer value expected?

I am running some commands and I am trying to get an output into a variable. I am having problem when I try to put that value in while loop, it says integer value expected. What's the best way to accomplish this remaining=$(symclone -sid XXX -f Clone_test query | grep MB | awk '{print... (1 Reply)
Discussion started by: rajsan
1 Replies

5. Shell Programming and Scripting

using awk compare two variables in bash

ok this is probably going to turn out to be something really stupid but i've tried to use the following command in a script but the output is just a blank screen and i have to use Ctrl c to exit it. awk 'BEGIN {printf "%.2f\n", '${bashArray}'>='$Variable' {print '${bashArray}'}}' the command... (2 Replies)
Discussion started by: zagreus360
2 Replies

6. Shell Programming and Scripting

HELP: compare floating point variables??

Hi All, I got this script that pulls the Amps value from our RPC's , I basiclly want to compare the valued with a "limit" value -- if the numbers match or are greater than the definded value ...do something. My problem is I cant seem to figure out how to compare floating points... here is... (1 Reply)
Discussion started by: zeekblack
1 Replies

7. Shell Programming and Scripting

Awk - Compare fields and increment variables

Hi, My first post to this group... I have a need to to parse a source file which is a capture from a network analyser. I have two fields that need to be checked: - Field 7 represents the packet length (an integer), and Field 4 represents a network address (e.g. 192.168.25.3) - The... (10 Replies)
Discussion started by: mv652
10 Replies

8. Shell Programming and Scripting

Compare String Variables for Greater or Less Than?

Is there any way to compare two strings for a greater or less than condition? For example, StringA is "apple" and StringB is "bonnet" and StringC is "captain". Can I only test for equal/not-equal, or is there a way to find out whether StringA is less than StringB, and StringC is greater than... (6 Replies)
Discussion started by: OPTIMUS_prime
6 Replies

9. Shell Programming and Scripting

Compare two variables

Hi guys, How do I compare two variables using diff? The way I'm thinking: #!/bin/sh a=cat asdf.x | wc -l b=cat asdf.y |cut -d',' -f5 |grep -v '^$' |wc -l diff $a $b How to rewrite the above sciprt using only one line in command prompt? (2 Replies)
Discussion started by: onthetopo
2 Replies

10. Shell Programming and Scripting

How do I compare mixed variables?

Searched all over can't figure out how to compare variables of alpha and numeric characters. Example a script that ask user to enter a password and then ask to repeat the password. (6 Replies)
Discussion started by: monx
6 Replies
Login or Register to Ask a Question