home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / question / 9413 < prev    next >
Encoding:
Text File  |  1992-07-25  |  1.7 KB  |  81 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!casbah.acns.nwu.edu!navarra
  3. From: navarra@casbah.acns.nwu.edu (John Navarra)
  4. Subject: Argh! Regular expressions with [ef]grep
  5. Message-ID: <1992Jul25.035016.17217@news.acns.nwu.edu>
  6. Sender: usenet@news.acns.nwu.edu (Usenet on news.acns)
  7. Organization: Northwestern University, Evanston Illinois.
  8. Date: Sat, 25 Jul 1992 03:50:16 GMT
  9. Lines: 70
  10.  
  11. I have two related questions on regular expressions using some form of
  12. grep. They both involve grouping the pattern with ()'s and doing some
  13. form of counting of the number of occurences of the pattern within
  14. the paranthesis.
  15.  
  16.  
  17.     Say I have the following file:
  18.  
  19. 0
  20. 00
  21. 000
  22. 0000
  23. 00000
  24. 000000
  25. 0000000
  26. ...
  27.  
  28.     and I issue the following commands:
  29.  
  30. $ grep '^0\{1\}$' testfile 
  31. 0
  32. $ grep '^0\{2\}$' testfile 
  33. 00
  34. $ grep '^0\{1,3\}$' testfile 
  35. 0
  36. 00
  37. 000
  38.  
  39. Now, however, I want to change each '0' to a '123':
  40.  
  41. 123
  42. 123123
  43. 123123123
  44. 123123123123
  45. 123123123123123
  46. 123123123123123123
  47. 123123123123123123123
  48.  
  49. and I want to get the same results with the pattern matching.
  50. I want to print out the lines with one string of '123', the 
  51. lines with two strings of '123',  or at least 1 but no more than
  52. 3 strings of '123'  
  53.  
  54. Is there some way using a form of grep to do this? Egrep has '()' grouping
  55. but doesn't have the \{n,m\} stuff. And grep has the opposite. Grrr. 
  56.  
  57. Secondly, what if my file looked like:
  58.  
  59. 123.105.113.13
  60. 123.123.113.13
  61. 123.123.123.13
  62. 123.123.123.123
  63.  
  64. How would I print out the lines matching the following conditions:
  65. 1) only one occurence of '123'
  66. 2) two or more occurences of '123'
  67. 3) at least two but no more than 4 occurences of '123'.
  68.  
  69.  
  70. Is grep disabled or am I?
  71.  
  72. -tms
  73.  
  74.  
  75. ~
  76. ~
  77. -- 
  78. From the Lab of the MaD ScIenTiST:
  79.                     
  80. navarra@casbah.acns.nwu.edu        
  81.