Re: Is it fair to say, "sed supports # comments starting any column"?

"Tim Chase [email protected] [sed-users]" <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
On 2015-08-11 22:37, Daniel Goldman wrote:
> - The POSIX spec, at the link I sent, does not say "# is a
> command". If anyone has a POSIX link with that wording, please post
> it. Instead,
> http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html
> says, "The comment character is treated as a command, and it has
> the same properties in terms of being accepted with leading <blank>
> characters". The point is that # can have leading blanks.
> 
> The POSIX text is confusingly worded. What else is new? Even the
> GNU sed manual sloppily lumps in # with other "commands". But a
> comment is not a command. It's a comment.

Well, that link does lump "#" in with other commands:

"""
Command verbs other than {, a, b, c, i, r, t, w, :, and # can be
followed by a <semicolon>, optional <blank> characters, and another
command verb.
"""

so it seems to be pretty clearly indicating that "#" is a "command
verb" on par with other command-verbs, even if that command's effect
is "ignore to the EOL".

And to back that up, the definition of "#" is listed under the
"Editing commands in sed" section, not some "other syntactic elements
that are not commands".

> - The POSIX spec, at the link I sent, does not say "it (#) must be 
> separated from another command with ; or newline". If anyone has a 
> reference that says what you quoted, please post it. I think the 
> standard allows "s/x/y/ # comment" format.

But when you read that "#" is considered a command, then the
guideline that commands be separated by semicolons is implied.

> - The POSIX spec, at the link I sent, does not mention ; syntax. If 
> anyone has a POSIX link that mentions ; syntax, please post it.

I quoted the semicolon syntax from the link you gave.


> Thanks for posting to the POSIX discussion group. My understanding
> of your post and the replies is that solaris will at least be
> updated to allow blanks before the # character, and that the
> intention is that it be POSIX compliant, whatever that means. I
> think "s/x/y/ # comment" is POSIX compliant. Does anyone disagree,
> and if so, on what basis?

See the quote above. "#" is a command, and when chaining commands, it
requires a ";"

> Is my understanding correct, based on "SVID which only allows a
> comment as the first line of the script", and what Rakesh said,
> that System V only allows the line 1 of the script to be a comment?
> If so, isn't that incredibly pitiful, and no basis for any kind of
> argument? It seems extremely primitive, which is saying a lot
> coming from a seder. :)

Quoting that linked spec

"""
The requirements for acceptance of <blank> and <space> characters in
command lines has been made more explicit than in early proposals to
describe clearly the historical practice and to remove confusion
about the phrase "protect initial blanks [sic] and tabs from the
stripping that is done on every script line" that appears in much of
the historical documentation of the sed utility description of text.
(Not all implementations are known to have stripped <blank>
characters from text lines, although they all have allowed leading
<blank> characters preceding the address on a command line.)

The treatment of '#' comments differs from the SVID which only allows
a comment as the first line of the script, but matches BSD-derived
implementations. The comment character is treated as a command, and
it has the same properties in terms of being accepted with leading
<blank> characters; the BSD implementation has historically supported
this.
"""

So my reading is that modern compliant implementations must allow for
leading blanks, but that historical implementations can require
the comment char to begin in the first column.

> The GNU sed online manual does not list "comments can start in any 
> column" as an GNU extension. Apparently, the maintainers consider
> GNU sed # behavior to be POSIX compliant. To me, combined with the
> POSIX wording I have seen so far, that indicates the answer to my
> question, "is it fair to say sed supports # comments starting any
> column?" is "YES". Would anyone argue "NO", and on what basis?

As above, the only acceptable "NO" would be "I'm a[n] historical
implementation".

> To my knowledge, 99% to 100% of languages do not require a command 
> separator before a comment.

Welcome to one of the ones that does.  Like some BASICs and .bat
files where REM is treated as a command and thus must come in a
command context.


> Thus, my answer to "must sed # comments be separated from commands
> on the same line by ; syntax?" is "NO". Would anyone argue "YES",
> and on what basis?

I would argue that technically it must be because the spec treats "#"
as a command and thus it requires a command-separator.  GNU sed
appears to relax that requirement and allow a comment to start after
a non-consume-to-end-of-line command.

-tim
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.