Compare two variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two variables
# 1  
Old 05-11-2007
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  
Old 05-12-2007
Why do you want to use diff ?
You can simply use if :
Code:
#!/bin/sh
a=`wc -l <asdf.x` 
b=`cut -d',' -f5 <asdf.y | grep -v '^$' | wc -l`
if [ $a -ne $b ]
then
   echo "Difference found : $a / $b"
fi

The code in one line :
Code:
[ `wc -l <asdf.x` -ne `cut -d',' -f5 <asdf.y|grep -v '^$'|wc -l`] && echo "Difference found"

Another solution using awk :
Code:
awk -F, '
   NR==FNR { ++count_1 ; next}
   NF>0 { ++count_2;}
   END { if (count_1 != count_2) printf("Difference found : %d / %d", count_1, count_2) }
   ' asdf.x asdf.y

Jean-Pierre.
# 3  
Old 05-12-2007
Quote:
Originally Posted by onthetopo
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

[...]
With bash/ksh93:

Code:
diff <(echo "$a") <(echo "$b")

... and, of course, the cat command in your code is useless.
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

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 paste file1 file2 | awk '{print $1 - $2}' I want to make it... (8 Replies)
Discussion started by: faka
8 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. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
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