RE: sed "Hello, world!" program?

<[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
That is a good point that --version could write to stderr, and would certainly if no valid --version option, so maybe 2>&1 needed. All the Linux commands seem to write --version to stdout, but there is no way to know (for me) to know about all other possible sed programs, or even which ones (if any) have --version option. I would pick stdout for --version output, because --version output is not any kind of error, it is exactly what the user wants. But someone else might pick different, there is apparently no "rule" about this.

Related to which have --version http://www.unix.com/man-page/FreeBSD/1/sed/ says freebsd sed does not support --version option period. Other freebsd utilities I checked (ls and cp) also do not support --version option. It seems they don't support any options in "long syntax" form. Apparently, ditto for open solaris sed, from the same man page lookup site. This is probably not news to many here. Maybe GNU sed is the only one with --version option?

To me, it would seem preferable NOT to use 2>&1 for "Hello, world!" example. 2>&1 seems rather obscure, certainly not simple, and has little or no connection with the intrinsic nature of sed (IMO). I do a lot of shell scripting, am very familiar with 2>&1, sometimes use it. But I think (really am sure) it would confuse and discourage beginning users, make sed look even more cryptic than it can sometimes be. There is even some disagreement about details of 2>&1 use on this group of relatively expert users. I was also not aware that 2>&1 works under cmd.exe environment, which I use a fair amount. It doesn't surprise me it works, makes sense it works, I just did not know that it did.

//////////////////////

Anyway, here's a suggestion for consideration. Maybe there could be two sed "Hello, world!" versions:

1) sed -e 's/.*/Hello, world!/' -e 'q' non­_empty_file (any sed)

2) sed --version | sed 's/.*/Hello, world!/; q' (GNU sed only)

Any thoughts about having these two versions? Some of the "Hello, world!" examples for other languages have more than one version, depending on some factor. So I don't think there would be a problem having two versions.

The advantages would be (condensed from previous post):

1) Unique. No other language has "Hello, world!" even vaguely similar.
2) Like sed (at its best), relatively simple and short one-liners.
3) Emphasizes a unique attribute of sed, that it MUST have input.
4) Does not rely on "echo" or other (possibly missing) non-sed command.
5) Highlights key s command, but also shows simple programming.
6) Shows that script needs to be quoted if an included blank.
7) Shows input stream can be from a file or from a pipe.
8) Shows both semicolon syntax and merging -e options.
9) Uses simple regular expression syntax, key to using sed.

BTW, a "Hello, world!" does not have to be perfect. Given the variety of sed versions, perfection is probably not attainable here. I'm mostly a C programmer, and C does whatever I want. But I still see discussions and disagreements about (admittedly minor) details of the C "Hello, world!" program.

Daniel
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.