LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-11-2006, 10:14 AM   #1
loopoo
Member
 
Registered: Aug 2005
Posts: 40

Rep: Reputation: 15
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
 
Old 12-11-2006, 10:40 AM   #2
mjgould
LQ Newbie
 
Registered: Sep 2005
Location: Findlay Ohio
Distribution: CentOS, Ubuntu
Posts: 24

Rep: Reputation: 15
What is it that you are looking to output from comparing the values? For example are you looking to output the number of lines in a file that starts with "+-". Knowing what type of information you are looking to gather will help me understand what type of expression you are looking for.

~Mike
 
Old 12-11-2006, 10:46 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603Reputation: 3603
declare var="+-something"
Variable substring check in Bash (pure) you can use
Code:
[ "${var:0:2}" = "+-" ] && echo OK
If you want / need compatibility use for instance
Code:
case $var in +-*) echo OK;; esac
 
Old 12-11-2006, 10:47 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 67
Code:
if echo "$P1" |grep -q '^+-'; then 
    echo "P1 begins with +-"
else 
    echo "P1 does not being with +-"
fi
 
Old 12-11-2006, 10:49 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 67
unspawn's solution is better - no external program invoked. I feel foolish now for using grep. *blush*

On the bright side, this is my 1000'th post! Post party!
 
Old 12-11-2006, 11:13 AM   #6
loopoo
Member
 
Registered: Aug 2005
Posts: 40

Original Poster
Rep: Reputation: 15
Thank you all for your help.

I prefered unspawn solution.
I had to change from sh to bash...because of the string splitting

Once again, thanx!
 
Old 12-11-2006, 11:51 AM   #7
loopoo
Member
 
Registered: Aug 2005
Posts: 40

Original Poster
Rep: Reputation: 15
Another small question

I need one more small thing: before this check, i want to verify first if the string variable has at least 2 characters (without doing this check i get the following error: "[: =: unary operator expected")

Thanks,
Chris
 
Old 12-11-2006, 12:17 PM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 67
Quote:
Originally Posted by loopoo
I need one more small thing: before this check, i want to verify first if the string variable has at least 2 characters (without doing this check i get the following error: "[: =: unary operator expected")

Thanks,
Chris
Make sure you quote the test variable, i.e. do this:
Code:
[ "${var:0:2}" = "+-" ] && echo OK
not this:
Code:
[ ${var:0:2} = "+-" ] && echo OK
 
Old 12-11-2006, 12:35 PM   #9
loopoo
Member
 
Registered: Aug 2005
Posts: 40

Original Poster
Rep: Reputation: 15
Thanks for your suggestion....

i switched meanwhile to on another solution using "case" for tests.


Chris
 
  


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
shell script question: automatic \ of escape characters in string lagu2653 Programming 2 03-08-2006 08:58 AM
compare string in SQL hus Programming 9 04-27-2005 11:17 AM
exploding string into individual characters using a shell script farmerjoe Programming 9 10-13-2004 04:23 AM
find repeated characters in a string mcshen Programming 9 02-02-2004 06:43 PM
compare string in C++ danxl Programming 3 11-02-2003 03:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:01 PM.

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
Open Source Consulting | Domain Registration