If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Compare first characters of a string in UNIX shell

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-11-06, 10:12
loopoo loopoo is offline
Registered User
 
Join Date: Aug 2005
Posts: 23
Compare first characters of a string in UNIX shell

Hello all!

I want to verify if the first 2 characters of a string are "+-".
Can you please help me with that? How can I compare if the varaible (let's say P1 begins with "+-" )


Thanks in advance,
Chris
Reply With Quote
  #2 (permalink)  
Old 12-11-06, 13:28
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Talking


Try this:
Code:
[[ `echo $P1|cut -c1-2` == '+-' ]] && echo 'Yes.'

__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #3 (permalink)  
Old 12-12-06, 02:41
loopoo loopoo is offline
Registered User
 
Join Date: Aug 2005
Posts: 23
Thanks a lot for your help!

Chris
Reply With Quote
  #4 (permalink)  
Old 12-12-06, 03:10
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
Code:
case "$p1" in
  +-*) echo "Start with +-" ;;
    *) echo "Doesn't start with +-" ;;
esac
Reply With Quote
  #5 (permalink)  
Old 12-12-06, 03:12
loopoo loopoo is offline
Registered User
 
Join Date: Aug 2005
Posts: 23
Hi!

In fact pdreyer's solution is the one I have chosen already!

Thank you all!
Reply With Quote
  #6 (permalink)  
Old 12-13-06, 04:25
5h4rk 5h4rk is offline
Registered User
 
Join Date: Dec 2006
Posts: 1
Oh, I'm having a similar problem, I need to compare 2 or more characters (letters) to find out which is the largest or lowest one.

For example, I enter each letters one by one, like a, b, c, d, e, so the largest one is e and the lowest one is a.

How can I do that?
Reply With Quote
  #7 (permalink)  
Old 12-13-06, 04:54
loopoo loopoo is offline
Registered User
 
Join Date: Aug 2005
Posts: 23
Hi!

I think you should parse the letters array and store the lowest.
For example

min="ZZZ" //or the first object of the array

for i in letters_array
do
if [[ $i < $min ]]
then
min=$i
fi
done


Hope that helps!
Chris
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On