Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

testing for regular expressions

Status
Not open for further replies.

jescat

Technical User
Jul 29, 2004
32
US
Is there a way in unix scripting to test variable using regular expressions similarly to awk or sed

For example:

awk:

$1 ~ /^[A-z].*$/ print "found regular expression"

Can you incorporate regular expression metacharaters in unix scripts
 
Hi there,

Yes, you can put regular expression metacharacters in scripts and spotting *some* re's will be fairly easy - but spotting *ALL* re's will be quite a task.

Tell us a bit more about what you're trying to achieve. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
I am not trying to accomplish anything in particular. It would just be nice to have the ability to test on re's like awk, sed or Perl. I figured there was but just didn't know the syntax. What were you thougthts?

Thanks
 
Oh I see, well - you can use the standard command 'grep' to use regular expressions in a UNIX shell script.

You can use grep to process files or its STDIN - so you can echo or print variables to grep's STDIN to check those. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
You can also use the expr command :


[ $(expr "$VAR" : "^[A-z].*$") -ne 0 ] && echo "found regular expression"



[COLOR=/blue]expr[/color] expr1 : expr2
The matching operator : compares the first argument
with the second argument which must be a regular
expression. expr supports the Basic Regular
Expression syntax (see regexp(5)), except that all
patterns are ``anchored'' (i.e., begin with ^) and,
therefore, ^ is not a special character, in that
context. Normally, the matching operator returns
the number of characters matched (0 on failure).
Alternatively, the \(...\) pattern symbols can be
used to return a portion of the first argument.


See the the man page ... Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top