home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / shell / 3101 < prev    next >
Encoding:
Internet Message Format  |  1992-07-28  |  4.2 KB

  1. Xref: sparky comp.unix.shell:3101 comp.unix.questions:9332
  2. Newsgroups: comp.unix.shell,comp.unix.questions
  3. Path: sparky!uunet!munnari.oz.au!metro!physiol.su.OZ.AU!john
  4. From: john@physiol.su.OZ.AU (John Mackin)
  5. Subject: Re: What's wrong with this sed command?
  6. Message-ID: <1992Jul23.090023.17661@physiol.su.OZ.AU>
  7. Summary: A lot
  8. Organization: Department of Physiology, University of Sydney, Australia
  9. References: <MONTNARO.92Jul21160321@ausable.crd.ge.com> <glaze.2520@glaze>
  10. Date: Thu, 23 Jul 1992 09:00:23 GMT
  11. Lines: 82
  12.  
  13. Below, > is someone called `Glaze' who seemed to go to some trouble to
  14. conceal their identity (either that, or their software is more broken
  15. than I can easily believe), and >> is Glaze quoting the original poster,
  16. whose identity Glaze chose to suppress -- and unfortunately my news
  17. machine doesn't have the original article.
  18.  
  19. >> I'm trying to use sed to trim off all leading "./" patterns from source
  20. >> filenames in make rules. On an IBM RS6000 the following works as I expected:
  21. >>
  22. >>    echo 'enquire.o: ./././enquire.c' | sed 's?: \(\./\)\{1,\}?: ?'
  23. >> yielding
  24. >>    enquire.o: enquire.c
  25. >>
  26. >> On our other systems (Sun-4, HP, SGI, and Motorola 68040) it prints
  27. >> different results. The HP doesn't trim any "./" patterns, while the Sun,
  28. >> Moto, and SGI all trim one pattern.
  29. >>
  30. >> Have I incorrectly specified the sed command or are one (or more) of the
  31. >> vendors' sed implementations incorrect? If so, which one(s)? I'm inclined to
  32. >> believe the IBM is correct and the others are bugged.
  33. >
  34. > I'm inclined to believe the IBM is incorrect and most of the others are
  35. > correct. Your sed should be trying to match ONE pattern of the form
  36. > ": ./"[blah]  and it does(on most machines) and replaces it. What you
  37. > want is for it to match multiple "./"s after the ": ", so try
  38. >    echo 'enquire.o: ./././enquire.c' | sed 's?: [\./]*?: ?'
  39.  
  40. Glaze's inclination is into error.  The real question the original
  41. poster has to ask himself (yes, I know it's a sexist usage, but I
  42. couldn't bring myself to write "themself") here is: do I want this
  43. sed script to be -portable-??  If you do, then you cannot do this in this
  44. way with sed (as I explain below).  If you only care about what it
  45. does on one particular machine, and you just want to know what
  46. behaviour is `correct', then you have to define your standard of
  47. correctness.
  48.  
  49. Now, some people might call me reactionary, but to me, the standard
  50. of correctness for Unix commands is the Seventh Edition manuals.
  51. These were published by HRW, and were once widely available; I don't 
  52. know if they are still in print.  I've looked in my copy, and neither
  53. the sed manual entry nor the sed paper in Volume 2 says one word about
  54. any \{m,n\} stuff.  Therefore, if I were writing a sed script that I
  55. wanted to be portable, I would not use such stuff.  For my money,
  56. the only one of the original poster's machines that is doing the
  57. right thing is the HP: it is all the others that have sed bugs here.
  58. (My Ultrix 4.2 DECstation (unsurprisingly -- DEC don't break the commands,
  59. in general) does the same thing, stripping nothing.)
  60.  
  61. As for Glaze's proposed solution, I am afraid it is bogus.  Yes, it
  62. will `work', in that it will strip as many ./'s as are there,
  63. but it will also strip, for example, "...///" -- it will strip
  64. _any combination of dot and slash (and backslash, see below)
  65. characters in the target position_, which is hardly what the
  66. original poster wanted.
  67.  
  68. The other error in Glaze's proposal involves the backslash inside
  69. the square brackets.  In a character class, there are no metacharacters,
  70. and writing a backslash introduces a backslash into the class.
  71. So the `correct' way to write Glaze's command would be:
  72.  
  73.     sed 's?: [./]*?: ?'
  74.  
  75. What the original poster wants cannot be done in one step with the ed
  76. subset of regular expressions (as used by sed).  If sed accepted full regular
  77. expressions, a la egrep, then the command would be:
  78.  
  79.     sed 's?: (\./)*?: ?'        WRONG WRONG WRONG
  80.  
  81. but since sed doesn't do full RE's, that won't work.  You need
  82. another approach.  Since you can't do it in one step, do it
  83. in several:
  84.  
  85.     sed -e ': loop' -e 's?: ./?: ?' -e 't loop'
  86.  
  87. Easy, and I will bet it will work in the great majority of vendor-busted
  88. sed's out there.
  89.  
  90. Moral: know your tools.
  91.  
  92. -- 
  93. John Mackin <john@physiol.su.oz.au>
  94. `Even the white bits are black.'
  95.