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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
12-11-2006, 10:14 AM
|
#1
|
Member
Registered: Aug 2005
Posts: 40
Rep:
|
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
|
|
|
12-11-2006, 10:40 AM
|
#2
|
LQ Newbie
Registered: Sep 2005
Location: Findlay Ohio
Distribution: CentOS, Ubuntu
Posts: 24
Rep:
|
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
|
|
|
12-11-2006, 10:46 AM
|
#3
|
Moderator
Registered: May 2001
Posts: 29,415
|
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
|
|
|
12-11-2006, 10:47 AM
|
#4
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
Code:
if echo "$P1" |grep -q '^+-'; then
echo "P1 begins with +-"
else
echo "P1 does not being with +-"
fi
|
|
|
12-11-2006, 10:49 AM
|
#5
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
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!
|
|
|
12-11-2006, 11:13 AM
|
#6
|
Member
Registered: Aug 2005
Posts: 40
Original Poster
Rep:
|
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!
|
|
|
12-11-2006, 11:51 AM
|
#7
|
Member
Registered: Aug 2005
Posts: 40
Original Poster
Rep:
|
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
|
|
|
12-11-2006, 12:17 PM
|
#8
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
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
|
|
|
12-11-2006, 12:35 PM
|
#9
|
Member
Registered: Aug 2005
Posts: 40
Original Poster
Rep:
|
Thanks for your suggestion....
i switched meanwhile to on another solution using "case" for tests.
Chris
|
|
|
All times are GMT -5. The time now is 03:01 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|