LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-19-2004, 02:58 AM   #1
rm1
LQ Newbie
 
Registered: Sep 2004
Posts: 5

Rep: Reputation: 0
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.
 
Old 09-19-2004, 04:30 AM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

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

http://www.geocities.com/tipsforlinu...cles2/043.html
 
Old 09-19-2004, 04:42 AM   #3
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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...???
 
Old 09-19-2004, 08:28 AM   #4
rm1
LQ Newbie
 
Registered: Sep 2004
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 09-19-2004, 08:50 AM   #5
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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..?
 
Old 09-19-2004, 09:24 AM   #6
rm1
LQ Newbie
 
Registered: Sep 2004
Posts: 5

Original Poster
Rep: Reputation: 0
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).
 
Old 09-19-2004, 09:46 AM   #7
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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.
 
Old 09-20-2004, 12:25 PM   #8
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
9.0 - 10.1 official comparision Nau Mandriva 1 04-11-2005 10:08 AM
comparision Ammad Linux - Networking 1 03-10-2005 06:26 AM
A picture comparision program? vmicho Linux - Software 3 10-27-2004 01:47 PM
Linux & Windows Boot process, Comparision ? spooler General 9 10-09-2004 06:02 AM
Windows/Linux comparision for science project danny_beta_read General 14 05-03-2004 12:19 PM


All times are GMT -5. The time now is 05:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration