home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / shell / 5251 < prev    next >
Encoding:
Text File  |  1993-01-05  |  2.3 KB  |  55 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!mcsun!sunic!palantir.p.tvt.se!infolog!mich
  3. From: mich@lin.infolog.se (Thomas Michanek)
  4. Subject: Extracting regular expressions using egrep/sed/...
  5. Message-ID: <1993Jan5.135145.28405@infolog.se>
  6. Sender: news@infolog.se
  7. Organization: Infologics, Linkoping, Sweden
  8. Date: Tue, 5 Jan 1993 13:51:45 GMT
  9. Lines: 44
  10.  
  11.  
  12. I have two questions concerning csh programming:
  13.  
  14. 1. Let's say I have a variable $var containing >1 words of text.
  15.    I want to know if this variable contains a specific pattern in the
  16.    form of a regular expression. The way I do it now is:
  17.  
  18.      if ("`echo $var | egrep '<regexp>'`" != "") then ...
  19.  
  20.    I have to double quote since egrep returns >1 word when matching.
  21.    Otherwise I get an 'if: expression syntax' error.
  22.  
  23.    Is there an easier and/or faster way to do this test? (in csh please!)
  24.  
  25.  
  26. 2. Now that I know the variable contains what I want, I want to extract
  27.    the part(s) of the variable value that matches the regular expression.
  28.    Let's say I'm looking for integers (i.e. [0-9]+) and $var contains:
  29.  
  30.      Is 54 greater than 33?
  31.  
  32.    I want to extract the values "54" and "33" (not "33?") out of this string.
  33.    How do I do that? egrep can't help since it returns the whole string.
  34.    I've looked at sed and tried to find a way to delete the characters
  35.    _not_ matching the regexp, something like "sed 's/!regexp//g'". I've
  36.    read about the 'hold space' in sed and replacing the 'pattern space'
  37.    with that, but the 's' command doesn't seem to use the 'hold space'?
  38.    And yes, I know that for the simple example above I could do:
  39.  
  40.      echo $var | sed 's/[^0-9 ]//g'
  41.  
  42.    but what if the regexp is very complicated? Please note that the regexp
  43.    may span over several words, so I can't loop through each word in $var
  44.    and see if it matches.
  45.  
  46.  
  47. Any pointers are much appreciated!
  48.  
  49. .- - - - - - - - - - - -.  _ _  _ ___ _  _   _   _  _  _  _   .- - - - - - - -.
  50. |    Thomas Michanek    |  | |\ | |_ / \ |  / \ / _ | / `|_`  | Dial +46 13   |
  51. |  Infologics Linkoping |  | | \| |  \_/ |_,\_/ \_| | \_,._|  | 210060  Phone |
  52. | email:Thomas.Michanek |          _                          | 210068  Direct|
  53. |       @lin.infolog.se |  L I N K O P I N G  -  S W E D E N  | 210155  Fax   |
  54. `- - - - - - - - - - - -'                                     `- - - - - - - -'
  55.