RE: sed "Hello, world!" program?
Rakesh Sharma <[email protected]>
| Newsgroups | gmane.editors.sed.user |
|---|---|
| Message-ID | <[email protected]> |
I see two issues with "hello, world" thingy that you have:
Not all seds support "--version" option. And as has been pointed out already, even when they do, they all might not dump the output of "--version" onto stdout. To take care of that you have to do redirection. But that depends on what shell you are in. The syntax 2>&1 doesn't work in tcsh.
The ! character will cause the construct to blow up in case the shell is tcsh/csh. (It error out complaining the event / not found.)
My suggestion is the following:
% yes | sed -e 's/.*/Hello, world\!/' -e 'q'
This takes makes it shell-agnostic (we backslash the !, so it satisfies both csh & bourne flavors)
The command "yes" dumps it output on stdout & so we no more rely on the "--version" option being present on the sed version.
Thanks,
Rakesh
To: [email protected]
From: [email protected]
Date: Fri, 21 Feb 2014 10:47:01 -0800
Subject: Re: sed "Hello, world!" program?
Thanks for the reply. I don't see a huge burst of interest from other group members. :)
Anyway, taking into account your suggestions, and after a little thought, I would like to suggest the following as candidate for "official" :) sed "Hello, World!":
sed --version | sed 's/.*/Hello, world!/; q'
This "Hello, world!" has the following advantages:
1) Like sed, it is unique. No other language has a "Hello, World!" even vaguely similar.
2) Like sed, it is relatively simple and short, one line.
3) It points out a unique attribute of the sed "language", that it MUST have input.
4) It does not rely on "echo" or other non-sed command (that may not be present). It makes it's own input, whatever text --version happens to produce. Maybe some sed versions may not have --version option? But it is a standard command line option, so I think it is fine for this purpose.
5) It does a good job demonstrating sed: It uses the key s command. And it shows how to write a simple program to quit after processing the first line.
6) It shows the script needs to be quoted if there is an included blank.
I take it some sed versions do not support semicolon. So I would suggest also having an alternate form:
sed --version | sed -e 's/.*/Hello, world!/' -e 'q'
Advantages of the alternate "Hello, world!" are that it shows how multiple scripts can be combined into one script, and it shows the use of a sed command line option that might be used in real operation.
Anyway, if I don't hear back after a week or so, I will try adding to wikipedia.
Daniel