LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 04-11-2013, 01:57 AM   #1
rajivswe.2k7
LQ Newbie
 
Registered: Apr 2013
Posts: 15

Rep: Reputation: Disabled
Regex in shell.


I have tried to use regex in shell.But i got the following error.Please advise.

val='rajiv'; if [ ${val} ~ /ji/ ];then echo matched; fi

-bash: [: /home/rgandhi: binary operator expected

Please advise.
 
Old 04-11-2013, 01:59 AM   #2
rajivswe.2k7
LQ Newbie
 
Registered: Apr 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
Could you please suggest any good material for shell regex....
 
Old 04-11-2013, 02:03 AM   #3
rajivswe.2k7
LQ Newbie
 
Registered: Apr 2013
Posts: 15

Original Poster
Rep: Reputation: Disabled
I got resolve. Please advise any other better method if you have.

val='rajiv'; if [[ ${val} =~ ji ]];then echo 'i am here'; fi
 
Old 04-11-2013, 06:08 AM   #4
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
Code:
val='rajiv';
if [[ "$val" =~ ji ]];then
   echo "i am here";
fi

Last edited by whizje; 04-11-2013 at 06:33 PM.
 
1 members found this post helpful.
Old 04-11-2013, 04:49 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


Regex evaluation is only supported inside [[..]] brackets.

For all but the most trivial expressions you should save the regex to a separate variable first. Otherwise you have to struggle to escape all the reserved characters from shell parsing. You don't need any delimiting characters like "/../", and don't quote the regex variable inside the brackets.

Code:
string=foobarbazbum
re='^...(.*)...$'
[[ $string =~ $re ]] && echo "the middle part of $string is '${BASH_REMATCH[1]}'"
Finally, if all you want is a simple substring match, forget the regex and use a globbing pattern instead. This can again be done inside [[..]], or even better, with a case statement.

Code:
string='rajiv'

if [[ $string == *ji* ]]; then
    echo 'found string'
fi

case $string in
    *ji*) echo 'found string' ;;
esac

Last edited by David the H.; 04-11-2013 at 04:58 PM. Reason: expanded answer
 
1 members found this post helpful.
  


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
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
execute script shell with regex as argument eguider Linux - Newbie 2 12-15-2009 07:26 PM
regex in shell scripts vinaytp Linux - Newbie 4 11-25-2009 03:58 AM
Shell regex - add new field czezz Programming 3 02-28-2009 03:13 PM
performing regex on shell script variable? ocicat Programming 3 07-17-2007 02:10 PM


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