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

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