[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: comparing string with regular expression using test command in unix
From: |
Eric Blake |
Subject: |
Re: comparing string with regular expression using test command in unix |
Date: |
Mon, 20 Feb 2006 21:15:10 +0000 |
>
> Hi,
>
> I want to compare a variable holding *string* with the *regular
> expression *. Is it possible to do in unix with a single command?
> Can we use "test" command in UNIX to compare a *string *with the
> *regular expression*? if so please provide an example for that.
This sort of question generally implies that it would be well
worth your investment in a good book on Unix shell programming.
In coreutils, 'info expr' may prove enlightening. For example,
$ foo=bar
$ expr $foo : 'ba*' && echo found || echo not found
2
found
$ expr $foo : 'ca*' && echo found || echo not found
0
not found
Outside of the realm of coreutils, there are other
techniques. For example, in /bin/sh, you can do glob
matching (instead of regular expressions) like this:
$ case $foo in ba*) echo found ;; *) echo not found ;; esac
found
Or use the regular expressions in sed:
$ echo $foo | sed -ne '/ba*/p'
bar
$ echo $foo | sed -ne '/ca*/p'
$
Or several other ideas. Hopefully this gives you some ideas.
>
> SASKEN BUSINESS DISCLAIMER
> This message may contain confidential, proprietary or legally Privileged
> information. In case you are not the original intended Recipient of the
> message,
> you must not, directly or indirectly, use, Disclose, distribute, print, or
> copy
> any part of this message and you are requested to delete it and inform the
> sender.
Sorry, but this email is now in the public domain. You should consider
using an account that does not have such unenforceable disclaimers
when posting to public mailing lists.
--
Eric Blake
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Re: comparing string with regular expression using test command in unix,
Eric Blake <=