[comp.unix.questions] Interaction of sed N and d commands

daniel@ora.UUCP (Daniel Gilly) (04/28/88)

I need help with the interaction between the sed N command and other
commands.  Note that I solved the problem presented here by making two
passes with sed; I don't need solutions (although particularly elegant
ones are always appreciated).  What I am trying to do is to
understand why sed acts the way it does.  I encountered some puzzling
behavior, and hope that someone out there can shed light on it.

I have a file containing these lines:

.SH "Related Commands"
.\"see also here
.br
\f(CWXRemoveHost\fP
.br
\f(CWXRemoveHosts\fP
.br
\f(CWXDisableAccessControl\fP
.br
\f(CWXEnableAccessControl\fP
.br
\f(CWXSetAccessControl\fP
.br
.\"last line comment

I've been having trouble using sed to get it to look the way I want.
First, I want to remove the .br's.  Then, I want to add a comma after
each X function, except the last one, which should have a period.

I've tried using the N command but I get unexpected results.

When I tried this:                       I got this:

/"Related Commands"/,/last line/{        .SH "Related Commands"
/\.br/d                                  .\"see also here
/fP/{                                    \f(CWXAddHost\fP
N                                        .br,
s/$/,/                                   \f(CWXAddHosts\fP
s/fP *\(\n.*last line\)/fP.\1/           .br,
}                                        \f(CWXListHosts\fP
}                                        .br,
                                         \f(CWXRemoveHost\fP
                                         .br,
                                         \f(CWXRemoveHosts\fP
                                         .br,
                                         \f(CWXRemoveHosts\fP
                                         .br,
                                         \f(CWXDisableAccessControl\fP
                                         .br,
                                         \f(CWXEnableAccessControl\fP
                                         .br,
                                         \f(CWXSetAccessControl\fP
                                         .br,
                                         .\" last line comment


I understand that once N has been applied to a line, the next line is 
joined to that one, and not processed independently.  Nonetheless,
I would expect that the /.br/d command would cause the entire line to
be deleted.  Instead, it is ignored.

It seems that the N command supersedes the /\.br/d command.  That is,
instead of deleting .br's and then joining lines, it joins them first,
then ignores the delete.  Is there something obscure about the order
in which sed operates on a file?  I also tried moving the /\.br/d
line. 

When I tried:                           I got:

/"Related Commands"/,/last line/{       .SH "Related Commands"
/fP/{                                   .\"see also here
N                                       .\"last line comment
s/$/,/
s/fP *\(\n.*last line\)/fP.\1/
}
}
/\.br/d

This was expected, since the /\.br/ now matches a multi-line pattern.
But why don't these two scripts produce the same result, and why won't
sed let me delete a line if a join is later going to be applied to it?

-- 
Daniel Gilly (617) 527-4210
O'Reilly & Associates, Inc., Publishers of Nutshell Handbooks
981 Chestnut Street, Newton, MA 02164
UUCP:	uunet!ora!daniel      ARPA:   daniel@ora.uu.net