LinuxQuestions.org
Review your favorite Linux distribution.
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 12-18-2008, 06:52 PM   #1
ccin1492
Member
 
Registered: Feb 2004
Location: SoCal
Distribution: openSUSE 10.3
Posts: 139

Rep: Reputation: 15
Pattern matching in BASH.


I would like to write a simple if/else that checks if a variable holds a pattern. From the bash docs it seems you can check if it equals (=) or not (!=), but not if it contains a pattern.

For example, here's how I would write it in perl.

Code:
$var = "foobarfoo" ;

if ( $var =~ /bar/ ) {
   do something...
} else {
   do something else...
}
I can't seem to find an equivalent for bash. Any thoughts or ideas?

Many thanks and Happy Holidays!

Last edited by ccin1492; 12-18-2008 at 06:53 PM.
 
Old 12-18-2008, 07:03 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,666
Blog Entries: 54

Rep: Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952
# You could for example
[ "${var}" != "${var//bar/}" ] && echo yes
# or utterly convoluted [ $(expr match "${var}" ".*bar") -eq 6 ] && echo yes
# ?
 
Old 12-18-2008, 07:15 PM   #3
ccin1492
Member
 
Registered: Feb 2004
Location: SoCal
Distribution: openSUSE 10.3
Posts: 139

Original Poster
Rep: Reputation: 15
Ok, I think I understand your first example. But just for future reference, can explain what "${var//bar/}" is doing?

Thanks!
 
Old 12-18-2008, 07:41 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,666
Blog Entries: 54

Rep: Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952Reputation: 2952
var="foobarfoo"; echo "${var//bar/}"
# ?
 
Old 12-18-2008, 08:02 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
It's called "substring replacement"

Go to http://tldp.org and get the Advanced Bash Scripting Guide (Section 9.2--Manipulating Strings)
 
Old 12-18-2008, 08:12 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,540

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
Bash versions 3+ have pattern matching too, http://tldp.org/LDP/abs/html/bashver...#REGEXMATCHREF

Code:
$ [[ "foobarfoo" =~ bar ]] && echo yes
yes

Although I can't find evidence for this in the official manual...
 
Old 12-19-2008, 10:59 AM   #7
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi.
Quote:
Originally Posted by ntubski View Post
Bash versions 3+ have pattern matching too, http://tldp.org/LDP/abs/html/bashver...#REGEXMATCHREF

Code:
$ [[ "foobarfoo" =~ bar ]] && echo yes
yes

Although I can't find evidence for this in the official manual...
Perhaps:
Quote:
When the == and != operators are used, the string to the right
of the operator is considered a pattern and matched according to
the rules described below under Pattern Matching. The return
value is 0 if the string matches or does not match the pattern,
respectively, and 1 otherwise. Any part of the pattern may be
quoted to force it to be matched as a string.

An additional binary operator, =~, is available, with the same
precedence as == and !=. When it is used, the string to the
right of the operator is considered an extended regular expres-
sion and matched accordingly (as in regex(3)). The return value
is 0 if the string matches the pattern, and 1 otherwise.

-- excerpt from man bash, around line 330
cheers, makyo

Last edited by makyo; 12-19-2008 at 11:00 AM.
 
Old 12-19-2008, 11:29 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,305

Rep: Reputation: 175Reputation: 175
and of course you could use case
which is more like file glob pattern matching

Code:
case ${1} in 
    (*bar*)
       echo gotcha
    ;;
    (*)
       echo no bar 
    ;;
esac
 
Old 12-19-2008, 12:00 PM   #9
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
Be careful with Bash 3.2

Code:
[jc@home ~]$ echo $BASH_VERSION
3.00.15(1)-release
[jc@home ~]$ [[ "test123test" =~ "test(.*)test" ]]
[jc@home ~]$ echo ${BASH_REMATCH[0]}
test123test
[jc@home ~]$ echo ${BASH_REMATCH[1]}
123
I was stumped for a while on this one once. It's the reason I am very leery of non-1003 features.

Code:
jc@home:~$ echo $BASH_VERSION
3.2.39(1)-release
jc@home:~$ [[ "test123test" =~ "test(.*)test" ]]
jc@home:~$ echo ${BASH_REMATCH[0]}

jc@home:~$ echo ${BASH_REMATCH[1]}

jc@home:~$ shopt -s compat31
jc@home:~$ [[ "test123test" =~ "test(.*)test" ]]
jc@home:~$ echo ${BASH_REMATCH[0]}
test123test
jc@home:~$ echo ${BASH_REMATCH[1]}
123
 
  


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
pattern matching help needed. squirrel001 Linux - Newbie 2 06-17-2008 07:28 AM
pattern matching nadeemr Linux - Newbie 8 06-13-2007 12:05 PM
bash script pattern matching thedude2010 Programming 9 06-02-2006 03:39 AM
Pattern matching with a "case" statement in BASH King V Programming 8 04-27-2006 05:19 PM
Pattern Matching Help in Bash script cmfarley19 Programming 1 04-07-2004 10:22 AM


All times are GMT -5. The time now is 06:13 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration