[comp.unix.shell] sed man page error

marc@mercutio.ultra.com (Marc Kwiatkowski {Host Software-AIX}) (05/18/91)

In article <1991May16.085837.1368@ultra.com> marc@mercutio.ultra.com (Marc Kwiatkowski {Host Software-AIX}) writes:
>>   I know that 
>>
>>   sed -n '/PATTERN1/,/PATTERN2/p' filename
>>
>>   will print everything between pattern 1 & 2, but what if I only want the
>>   first occurence of these patterns in the file? I know I can pipe the above
>>   statement to sed '/PATTERN2/q' but there has got to be a way to do this
>>   in one sed process. Thanks.
> 	 sed -n '
> 	 /pattern00/ {
> 	:lbl00
> 	  n
> 	  /pattern01/ {
>		   q
>	   }
>	   p
> 	  b lbl00
>	  }'

Someone pointed out that the according to the documentation, this
solution shouldn't work.  Of course it does work, so I thought
I'd point out the sed documentation error that exists in every
sed man page I've ever seen.

From 4.3BSD URM sed man page:

	n Copy the pattern space to the standard output.  Replace
	  the pattern space with the next line of input.

It should read:

	n Copy the pattern space to the standard output unless
	  "-n" flag has been specified.  Replace
	  the pattern space with the next line of input.

It is surprising that this documentation error has existed for
so long, and has been reproduced so faithfully by numerous
Unices.  It makes one suspect that the additional features
of sed aren't used by many.  

If you'll allow me to get on my soapbox, I'd like to campaign
for sed.  sed's syntax is somewhat obtuse, but it takes far
less time to load than awk.  Since most of shell-script
execution time is spent creating pipes and filter-processes, this
can be a real win.  Context sensitive parsing is beyond the scope
of sed - well not really, but it starts to get very ugly -  however, 
I'd guess that the vast majority of filters 
written in awk are regular and could easily be recast in sed.

perl is probably better than awk or sed for regular filters,
since it provides both a rich set regular expression and flow
control commands, and it avoids the overhead of pipe and 
process creation.  The bourne shell is still the lingua franca 
of shell scripts, so until the time that perl become ubiquitous,
I think sed should be the filter of first resort.  I think 
a close, but gimletted, reading of the sed man page and a few
experimental sed scripts will convince the reader that more
is possible in sed than previously thought possible; especially
when one makes use of the hold space.

My two A cents










--
	------------------------------------------------------------------
	Marc P. Kwiatkowski			Ultra Network Technologies
	Internet: marc@ultra.com		101 Daggett Drive
	uucp: ...!ames!ultra!marc		San Jose, CA 95134 USA
	telephone: 408 922 0100 x249

	Ignore the following signature.
-- 
	------------------------------------------------------------------
	Marc P. Kwiatkowski			Ultra Network Technologies
	Internet: marc@ultra.com		101 Daggett Drive
	uucp: ...!ames!ultra!marc		San Jose, CA 95134 USA

maf@thor.dbsm.oz.au (Martin Foord) (05/29/91)

In article <1991May17.211650.2025@ultra.com> marc@mercutio.ultra.com (Marc Kwiatkowski {Host Software-AIX}) writes:
>I'd point out the sed documentation error that exists in every
>sed man page I've ever seen.
>
>From 4.3BSD URM sed man page:
>
>	n Copy the pattern space to the standard output.  Replace
>	  the pattern space with the next line of input.
>
>It should read:
>
>	n Copy the pattern space to the standard output unless
>	  "-n" flag has been specified.  Replace
>	  the pattern space with the next line of input.
>
>It is surprising that this documentation error has existed for
>so long, and has been reproduced so faithfully by numerous
>Unices.  It makes one suspect that the additional features
>of sed aren't used by many.  

Yes. I think so. Here are a few more bugs in the man page I've discovered :

:
:                 p           Print  the  pattern   space   if   a
:                             replacement was made.

Shouldn't this be something like :

	Print the pattern space if a replacement was made and the '-n' option
has been specifyed.

:
:                 w wfilename Write: append the pattern  space  to
:                             wfilename if a replacement was made.
:
...
:
:     (2)w wfilename
:                 Write: append the pattern space to wfilename.
:

And a nasty but subtle one in both of the above writes, should probably read
something like :

	Write :	Create a new file wfilename and then append the pattern space
to wfilename.

An unsuspecting reader of the manual could assume that w will NOT clobber
wfilename first but append on the already existing wfilename.
 
>If you'll allow me to get on my soapbox, I'd like to campaign
>for sed.  sed's syntax is somewhat obtuse, but it takes far
>less time to load than awk.  Since most of shell-script

Yes, I tend to agree here. The major problem with sed is as you say, the
arcane syntax, and lots of backslash regexs. Therefore it's not a trivial
exercise to learn the FULL capabilities of sed and so few do. It'd be good
to see this updated (eg: give sed a more egrepish like set of regexs), then
again one could basically be doing all this with perl -[p|n]e ...

Martin.
-- 
Martin Foord.				MHSnet/ACSnet:  maf@dbsm.oz.au
SBC Dominguez Barry.			Internet Gateway:
Phone: +61-2-258-2724			maf%dbsm.oz.au@munnari.oz.au