home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / shell / 5256 < prev    next >
Encoding:
Internet Message Format  |  1993-01-05  |  1.7 KB

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