LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-18-2011, 12:51 AM   #1
915086731
Member
 
Registered: Apr 2010
Posts: 144
Blog Entries: 6

Rep: Reputation: 2
Is there a command that can match a string with regular expression in linux ?


I want to find a command that can judge a string match the specified regular expression or not .
e.g. command "abc486de" "ab*\d*e\b"
Match, return success code
not match , return failed code, and promote why not match.
What is the command? Thanks!
 
Old 08-18-2011, 12:59 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
[[ abc486de =~ ab*\d*e\b ]] && echo yes || else echo no

That's for bash. is that what you're after? Not a particularly valid regex in your example though.
 
1 members found this post helpful.
Old 08-18-2011, 01:28 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
*nix is all about regex. Bash, find, grep, sed, awk, vi, and even less, can handle it in some form or other.

The exact solution you'll want to use depends on the context of your problem. Tell us what you need to do, and we'll help you figure it out.
 
1 members found this post helpful.
Old 08-18-2011, 02:02 AM   #4
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
Code:
[river@localhost new]$ [[ abc486de =~ ab*\d*e\b ]] && echo yes || echo no 
no
Thanks ,the above is what I want.
I had thought there exists a command that can do a match. Maybe I am wrong.

Does "&&" acts for "and", "||" acts for "or" ?
 
Old 08-18-2011, 02:05 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well that does do "a match" if it... matches... which that regex doesn't. some context about your actual requirements would probably help.
 
Old 08-18-2011, 02:56 AM   #6
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
I just want to judge if a string match a regular expression or not. I do not want to find or cut some line by using awk or grep. So "if" command is enough for me. But what's difference between "if [ ] " and "if [[ ]] "
Thanks.
 
Old 08-18-2011, 02:59 AM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
[ is a symlink to the /bin/test command whereas [[ is built in to bash syntax. Using [[ bash will understand the command as a logic comparison but using [ to bash it's just a bunch of characters it passes on like any other command line, expanding variables, causing whtespace issues, null value problems etc. use [[.
 
1 members found this post helpful.
Old 08-18-2011, 03:07 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
See here for the differences between [ and [[:
http://mywiki.wooledge.org/BashFAQ/031

BTW, bash's [ is also a shell built-in, but it mostly duplicates /bin/test. You still need to use [[ for advanced regex operation.

Last edited by David the H.; 08-18-2011 at 03:10 AM.
 
1 members found this post helpful.
Old 08-18-2011, 03:10 AM   #9
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
Quote:
but using [ to bash it's just a bunch of characters it passes on like any other command line, expanding variables, causing whtespace issues, null value problems etc. use [[
Sorry, I have never used [[ ]] , and I don't understand
Quote:
Using [[ bash will understand the command as a logic comparison but using [ to bash it's just a bunch of characters
Can you explain more ? Thanks!
 
Old 08-18-2011, 03:17 AM   #10
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
ok, so you have a variable, BOB:

BOB="hello I am a string"

if you run something like

if [ -z $BOB ] ...

then that would be expanded by bash before evaluation as by the [ / test command as

if [ -z hello I am a string ]

which is nonesense, as each word is taken to be a different parameter passed to [. SO you have to do ugly things like wrap the variable name in quote marks etc.

using [[, the test work is done by bash itself. it doesn't replace $BOB, it *understands* that $BOB is a variable being used as part of a logic test so evaluates it correctly within its own code, behaving much more like a normal programming language.
 
2 members found this post helpful.
Old 08-18-2011, 03:22 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
"[" is a command, a synonym for "test". That means that everything following it is just an argument to the command, like any other.

"[[" is a bash keyword, an internal function that follows special parsing rules. This means that it can do things and handle certain kinds of syntax that would choke the older command, such as regular expressions.

If you really want to learn things like this, start by reading this guide. It will give you all the basic concepts you need to know.
http://mywiki.wooledge.org/BashGuide
 
1 members found this post helpful.
Old 08-18-2011, 03:27 AM   #12
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
Sorry , I had studied http://mywiki.wooledge.org/BashFAQ/031, I understood now.
But "&&" after ]] seems act for "if then", "||" act for "else then", not the meaning of "and" "or" .
Code:
[[ abc486de =~ ab*\d*e\b ]] && echo yes || echo no
What's my understanding is
Code:
abc486de =~ ab*\d*e\b
,if success , run
Code:
echo yes
, if
Code:
ab*\d*e\b ]] && echo yes
error, run echo no.
I have it ?
 
Old 08-18-2011, 05:26 PM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Inside a test construct:

Code:
[[ x && y ]]
If x evaluates as true, then also evaluate y. If y is also true, then the whole expression is true. If either one evaluates as false, then the whole expression is false. So both x and y must be true for the test to succeed.

Code:
[[ x || y ]]
If x evaluates as false, then also evaluate y. If y is also false, then the whole expression is false. If either one evaluates as true, then the whole expression is true. So either x or y must be true for the test to succeed.



The behavior of the operators outside of the test construct is similar. It's not entirely equal to if-then-else, but it can usually be used as a shorthand for it.

Code:
x && y
If the exit code of command x (any command, not just test) is 0 (success), then also run command y. Otherwise skip it.

Code:
x || y
If the exit code of command x is >0 (failure) then also run command y. Otherwise skip it.

Code:
x && y || z
If x exits successfully, then run command y. Then if y exits successfully, skip z.

But if x fails, skip y. Then since the exit code of x is still >0, run z.

However, and this is the big difference, if x succeeds, and then y fails, then z will also run.

http://mywiki.wooledge.org/BashPitfa...d2_.7C.7C_cmd3

The thing to remember is that these two operators always base their actions on the status of the exit code of the command that preceded it, without consideration of the expression as a whole. && continues if the previous command succeeded, and || continues if the previous command failed.

As the link above points out, if you want to be completely safe, use a full if-then-else instead.
Code:
if x ; then y ; else z ; fi
 
1 members found this post helpful.
Old 08-22-2011, 02:45 AM   #14
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
Thanks all
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expression to match comments in C alexandnpu Linux - Newbie 1 07-10-2011 01:22 AM
sed regular expression match everything up to a certain character bhepdogg Programming 3 05-28-2009 02:59 PM
Don't match a regular expression dakensta Programming 7 09-21-2006 03:48 AM
perl regular expression a char match richikiki Programming 8 07-19-2006 03:37 AM
Regular expression to match a valid URL string vharishankar Programming 13 07-21-2005 09:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:50 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