home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- Path: sparky!uunet!mcsun!sunic!palantir.p.tvt.se!infolog!mich
- From: mich@lin.infolog.se (Thomas Michanek)
- Subject: Extracting regular expressions using egrep/sed/...
- Message-ID: <1993Jan5.135145.28405@infolog.se>
- Sender: news@infolog.se
- Organization: Infologics, Linkoping, Sweden
- Date: Tue, 5 Jan 1993 13:51:45 GMT
- Lines: 44
-
-
- 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. The way I do it now is:
-
- if ("`echo $var | egrep '<regexp>'`" != "") then ...
-
- I have to double quote since egrep returns >1 word when matching.
- Otherwise I get an 'if: expression syntax' error.
-
- Is there an easier and/or faster way to do this test? (in csh please!)
-
-
- 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 want to extract the values "54" and "33" (not "33?") out of this string.
- How do I do that? egrep can't help since it returns the whole string.
- I've looked at sed and tried to find a way to delete the characters
- _not_ matching the regexp, something like "sed 's/!regexp//g'". I've
- read about the 'hold space' in sed and replacing the 'pattern space'
- with that, but the 's' command doesn't seem to use the 'hold space'?
- And yes, I know that for the simple example above I could do:
-
- echo $var | sed 's/[^0-9 ]//g'
-
- but what if the regexp is very complicated? Please note that the regexp
- may span over several words, so I can't loop through each word in $var
- and see if it matches.
-
-
- Any pointers are much appreciated!
-
- .- - - - - - - - - - - -. _ _ _ ___ _ _ _ _ _ _ _ .- - - - - - - -.
- | Thomas Michanek | | |\ | |_ / \ | / \ / _ | / `|_` | Dial +46 13 |
- | Infologics Linkoping | | | \| | \_/ |_,\_/ \_| | \_,._| | 210060 Phone |
- | email:Thomas.Michanek | _ | 210068 Direct|
- | @lin.infolog.se | L I N K O P I N G - S W E D E N | 210155 Fax |
- `- - - - - - - - - - - -' `- - - - - - - -'
-