LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   string comparision in linux scripts (https://www.linuxquestions.org/questions/programming-9/string-comparision-in-linux-scripts-232390/)

rm1 09-19-2004 01:58 AM

string comparision in linux scripts
 
Hi,

I wanted help in string comparison in linux scripts.

I have two strings of different lenght namely

s1="abcdefgh"
s2="abcdef"


Now I want to compare string s2 with s1.

I know how to compare strings of same length but how to compare strings of
different length I am unaware.

Please help.

Ravi Modi.

jschiwal 09-19-2004 03:30 AM

This web site explains the bash programming basics. Use = for strings and -eq for numeric comparisons.

http://www.geocities.com/tipsforlinu...cles2/043.html

Proud 09-19-2004 03:42 AM

What exactly do you want the test to check? If you know they're different lengths, do you want to test if the shorter string is the first part of the longer or what? You already know they aren't equal strings...???

rm1 09-19-2004 07:28 AM

I actually want to check whether the first 6 characters of larger
string is equal to shorter string (the shorter string is of 6 characters).
I have above just given example & actually I am not aware of
the string contents before hand.

Proud 09-19-2004 07:50 AM

So you either want the 6 characters long substring from the beginning of the longer string (will it be know which is the longer?) and compare this new string to the second string, or just walk the first 6 chars of each string and compare each in turn..?

rm1 09-19-2004 08:24 AM

I am having two strings. one is shorter (s2) of 6 characters and another
is longer (s1) of 8 characters. I am knowing before hand which string
is shorter & which is longer. Also the length of shorter and longer string is known. Now I want to compare whether the
first 6 characters of longer string (s1) is equal to shorter string(s2).

Proud 09-19-2004 08:46 AM

A quick googling of bash string comparisons reveales this:
Quote:

Table B-5. String Operations
Code:

Expression        Meaning
${#string}        Length of $string
         
${string:position}        Extract substring from $string at $position
${string:position:length}        Extract $length characters substring from $string at $position
         
${string#substring}        Strip shortest match of $substring from front of $string
${string##substring}        Strip longest match of $substring from front of $string
${string%substring}        Strip shortest match of $substring from back of $string
${string%%substring}        Strip longest match of $substring from back of $string
         
${string/substring/replacement}        Replace first match of $substring with $replacement
${string//substring/replacement}        Replace all matches of $substring with $replacement
${string/#substring/replacement}        If $substring matches front end of $string, substitute $replacement for $substring
${string/%substring/replacement}        If $substring matches back end of $string, substitute $replacement for $substring
         
         
expr match "$string" '$substring'        Length of matching $substring* at beginning of $string
expr "$string" : '$substring'        Length of matching $substring* at beginning of $string
expr index "$string" $substring        Numerical position in $string of first character in $substring that matches
expr substr $string $position $length        Extract $length characters from $string starting at $position
expr match "$string" '\($substring\)'        Extract $substring* at beginning of $string
expr "$string" : '\($substring\)'        Extract $substring* at beginning of $string
expr match "$string" '.*\($substring\)'        Extract $substring* at end of $string
expr "$string" : '.*\($substring\)'        Extract $substring* at end of $string

* Where $substring is a regular expression.

mfeat 09-20-2004 11:25 AM

Code:

ff()
{
  echo -n $1 $2 "-> "
  if [ ${1:0:6} = $2 ]; then
    echo match
  else
    echo do not match
  fi
}

ff abcdefgh abcdef
ff zzzzzzzz abcdef

output:

abcdefgh abcdef -> match
zzzzzzzz abcdef -> do not match


All times are GMT -5. The time now is 07:19 PM.