Strings comparing incorrectly


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Strings comparing incorrectly
# 1  
Old 04-07-2012
Strings comparing incorrectly

Hello I'm very new to Linux and shell scripting so I only know basic stuff. I'm making a script with the purpose of finding the longest string or word in a file. Here's what I got so far:
Code:
#!/bin/bash 
longest=""
for i in $(strings -n $1); do

if  [[ $longest < $i ]]
then 
 longest=$i
fi
done
echo $longest

So basically the script will use a file given in the command line which contains words and then return the longest one.

This is the file I'm using to test it out it's called test:
Code:
apples
are
the
best
in
the
world

now I run it with bash -x script2 test
so I can see what's happening and this is what I get:
Code:
+ longest=
++ strings -n 1 test
+ for i in '$(strings -n 1 $1)'
+ [[ '' < apples ]]
+ longest=apples
+ for i in '$(strings -n 1 $1)'
+ [[ apples < are ]]
+ longest=are
+ for i in '$(strings -n 1 $1)'
+ [[ are < the ]]
+ longest=the
+ for i in '$(strings -n 1 $1)'
+ [[ the < best ]]
+ for i in '$(strings -n 1 $1)'
+ [[ the < in ]]
+ for i in '$(strings -n 1 $1)'
+ [[ the < the ]]
+ for i in '$(strings -n 1 $1)'
+ [[ the < world ]]
+ longest=world
+ echo world
world

So by the looks of it it's just replacing the value of the current word with the next one.
So my question is how can I accurately compare the words so it returns the longest one?
I'm guessing my mistake is in this line:
Code:
if  [[ $longest < $i ]]

Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-08-2012 at 01:45 AM..
# 2  
Old 04-07-2012
Code:
#! /bin/bash

longest=""
charCount=0

while read x
do
    if [ $charCount -lt ${#x} ]
    then
        charCount=${#x}
        longest=$x
    fi
done < $1

echo "Longest Word: $longest; Number of Chars: $charCount"

# 3  
Old 04-08-2012
The more modern syntax is
Code:
while read x
do
    if (( charCount < ${#x} ))
    then
        charCount=${#x}
        longest=$x
    fi
done < $1

# 4  
Old 04-08-2012
Minor nitpick:
Code:
done < "$1"

Otherwise it will break for filenames with spaces or strange characters..


Quote:
Originally Posted by SCB
[..]So by the looks of it it's just replacing the value of the current word with the next one.[..]
It is keeping the one that is lexically greater. If somehow the shorter string were padded with leading spaces to make them equal length, then your comparison would work, if you enclosed both variables in double quotes. But doing so would involve knowing the length of the larger of the two variables and that's what you were set to find out in the first place Smilie

Code:
$ [[ "bbb" < "aaaa" ]] && echo hello
$ [[ " bbb" < "aaaa" ]] && echo hello
hello


Last edited by Scrutinizer; 04-08-2012 at 03:13 AM..
# 5  
Old 04-08-2012
Works like a charm now. Thank you for the help Smilie, didn't know it would be so much easier using a while loop.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing Strings in ksh88

Hi I tried the following string comparison script in Ksh88 #!/bin/ksh str1='aC' str2='ABC' if then echo "Equal" else echo "Not Equal" fi Though str1 and str2 are not equal the script output says Equal . Please correct me Thanks (2 Replies)
Discussion started by: smile689
2 Replies

2. Shell Programming and Scripting

Help with Comparing 2 strings from text

Hey guys how do I compare 2 strings from the text file, and check for duplication? For example, I add an item call Laptop, it will record to the textfile call file. If it detects duplicate it will say the record record exist? file.txt contains Laptop:Sony:1000 Phone:Apple:30 A head... (4 Replies)
Discussion started by: aLHaNz
4 Replies

3. Shell Programming and Scripting

Comparing strings with sed

Input: The the the the Output: not-same same What would be the sed command to do this? (7 Replies)
Discussion started by: cola
7 Replies

4. Shell Programming and Scripting

Comparing strings using nawk

Hello All Please I have got a file called DATE.tex which consist of 01-04-2008_12:00:00 01-04-2005_12:00:00 01-04-2003_12:00:00 01-04-2007_12:00:00 01-04-2002_12:00:00 01-04-2009_12:00:00 I want to use nawk to print out the dates >=01-04-2009_12:00:00 I tried this cat plnt.new |... (6 Replies)
Discussion started by: ganiel24
6 Replies

5. Shell Programming and Scripting

comparing two strings

hi All i am facing prob in comparing two strings that have two word. below is the code snippet. checkValidates="file validates" file3_name="file" if then echo "file" $file3_name "is validated successfully" fi when i run this i get the error as -bash: [: too many arguments ... (1 Reply)
Discussion started by: infyanurag
1 Replies

6. Shell Programming and Scripting

comparing 2 strings

hi i have 2 strings. i want to compare the strings. please help (2 Replies)
Discussion started by: satish@123
2 Replies

7. Shell Programming and Scripting

comparing strings

i have a string in a file which gets repeated number of times like below: rpttxt("abc") . . rpttxt("REP_TITLE") rpttxt("BOS_TITLE") . . . . and so on using awk or grep how can i comapre the string( as the second half keeps varying) and store it in a temporary variable? I am using the... (3 Replies)
Discussion started by: agarwal
3 Replies

8. Shell Programming and Scripting

Comparing Two Strings

Hi All, While I am trying to run below code I Am getting the exception like ./abs.sh: line 102: syntax error near unexpected token `then' ./abs.sh: line 102: ` then' The Code Snippet is: if then cat $file1 | sed -e... (8 Replies)
Discussion started by: Anji
8 Replies

9. UNIX for Advanced & Expert Users

Comparing strings

I have two strings a=Mar22 b=may21 how can I compare them Is this fine if then; . ... else .... fi or if then (2 Replies)
Discussion started by: yakyaj
2 Replies

10. Shell Programming and Scripting

comparing two strings

Hi How do i compare two strings in shell script. Below is an example but I am not getting the desired output, plz help if then echo success fi I am not getting the desired output if I do this. plz help (24 Replies)
Discussion started by: ragha81
24 Replies
Login or Register to Ask a Question

Featured Tech Videos