Compare String Variables for Greater or Less Than?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare String Variables for Greater or Less Than?
# 1  
Old 02-03-2009
Data 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 StringA, etc.
Thanks!
# 2  
Old 02-03-2009
Which language?

Some options:
!=
<>
-ne
# 3  
Old 02-03-2009
In a .ksh script.
Remember, I'm not just looking for equal or not-equal. Need to know greater-than or less-than, in the sense of sorting or collating sequence.'
Thanks!
# 4  
Old 02-03-2009
Code:
#!/bin/ksh

str1='apple'
str2='bonnet'
str3='captain'

if [ "${str1}" = "${str2}" ]; then
   echo "[${str1}] = [${str2}]"
else
   echo "[${str1}] != [${str2}]"
fi

if [ $(expr ${str3} \<= ${str2}) -eq 1 ]; then
   echo "[${str3}] <= [${str2}]"
else
   echo "[${str3}] > [${str2}]"
fi

You can check the string 'equality' with the builtin 'test', but have to use something similar to 'expr' to check for the 'alphabetical' comparison.

'man expr' yields:
Code:
     expr{ =, \>, \>=, \<, \<=, !=} expr
           Returns the result of an integer  comparison  if  both
           arguments  are  integers, otherwise returns the result
           of a string comparison using the locale-specific coal-
           ition  sequence. The result of each comparison will be
           1 if the specified relationship  is  TRUE,  0  if  the
           relationship is FALSE.

# 5  
Old 02-03-2009
Quote:
Originally Posted by Ikon
Which language?

Some options:
!=
<>
-ne

None of which test strings for greater than or less than (i.e. lexical ordering), only for inequality.

In bash:

Code:
if [ "$string1" \< "$string2" ]; then

# 6  
Old 02-03-2009
cfajohnson,

The backslash before the relational operator did the trick. Without it, the script thought I was trying to create a file. This is what I have now, based on your suggestion:

if [ $file \> $last_file_loaded ]; then

Thanks!!!
# 7  
Old 02-03-2009
Quote:
Originally Posted by cfajohnson
None of which test strings for greater than or less than (i.e. lexical ordering), only for inequality.

In bash:

Code:
if [ "$string1" \< "$string2" ]; then

Opps, I had missread the question.


But who determines which is less than what with strings?

For example, StringA is "apple" and StringB is "bonnet" and StringC is "captain".
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. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

3. 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

4. UNIX for Beginners Questions & Answers

Compare first column from two csv files with greater than or equal, and less than

I have two csv files of different sizes. The output file needs to have file1 contents on top of file2 contents where file2 col1 is >= to file1 col1, and file2 col1(same value) is < file1 col1 (next value). So basically, some file2 rows will be matched to the same file1 row because it is the closet... (7 Replies)
Discussion started by: aachave1
7 Replies

5. 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

6. Shell Programming and Scripting

[Solved] Bash test 2 variables to see if ones greater by n

Experts, I have a bash shell script that generates 2 variables that have the current minute and a minute from a log file. Can someone please show me the best way to test if the minutes stray by 5. So basically if: This is ok: Last Fitting Min ============= 02 Current Minute =============... (2 Replies)
Discussion started by: jaysunn
2 Replies

7. Shell Programming and Scripting

compare files and remove a line from a file if first column is greater than 25

my files are as follows fileA sepearated by tab /t 00 lieferungen 00 attractiop 01 done 02 forness 03 rasp 04 alwaysisng 04 funny 05 done1 fileB funnymou120112 funnymou234470 mou3raspnhdhv rddfgmoudone1438748 so all those record which are greater than 3 and which are not... (4 Replies)
Discussion started by: rajniman
4 Replies

8. 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

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