home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!crdgw1!rdsunx.crd.ge.com!usenet
- From: csdgwh@sn370.utica.ge.com (Glenn W. Hoffman)
- Newsgroups: comp.unix.shell
- Subject: Re: Extracting regular expressions using egrep/
- Message-ID: <1993Jan5.195647.19715@crd.ge.com>
- Date: 5 Jan 93 19:56:47 GMT
- References: <1993Jan5.135145.28405@infolog.se>
- Sender: usenet@crd.ge.com (Required for NNTP)
- Organization: GE Corporate R&D Center
- Lines: 37
- Nntp-Posting-Host: xh620.utica.ge.com
-
- In article 28405@infolog.se, mich@lin.infolog.se (Thomas Michanek) writes:
- >
- > I have two questions concerning csh programming:
- >
- > 1. Let's say I have a variable $var containing >1 words of text.
- > I want to know if this variable contains a specific pattern in the
- > form of a regular expression.
- > ...
- > Is there an easier and/or faster way to do this test? (in csh please!)
- >
- egrep -s
- works silently, i.e., returns only a status that you may test in the
- if command. I am not a csh programmer, but it appears that you could say
- if (echo $var | egrep -s '<regexp>') then ...
-
- >
- > 2. Now that I know the variable contains what I want, I want to extract
- > the part(s) of the variable value that matches the regular expression.
- > Let's say I'm looking for integers (i.e. [0-9]+) and $var contains:
- >
- > Is 54 greater than 33?
- >
- I would use a fragment like the following in this case:
- eval `echo $var | sed -n \
- -e 's/.*\([0-9][0-9]*\).*\([0-9][0-9]*\).*/set VAR1=\1;set VAR2=\2/p'`
-
- The two numbers you want will be in variables VAR1 and VAX2.
-
- No guarantees, of course, that this untested code is 100% correct, but it was
- derived from a working (sh) procedure.
-
- >
- > Any pointers are much appreciated!
- >
- You're welcome. Hope this helps.
-
- Glenn
-