Note one can also add -n to the sed command,
to get it to strip the header entirely.
The following indeed works with sed 4.2.2 ( on linux 3.2 ):
$ ( echo 99 ; seq 10 ) | ( sed -u 1q ; sort -n )
But I'm wondering (as per the link above [2]) if this is posix compliant and
stable (i.e. can this be trusted to work everytime, even on non-linux
machines?).
No `sed -u` with this functionality is not portable.
Though it's more portable than `sort --header`
given that it already exists :)
Thanks for collating the previous threads on this subject.
I'm on the fence on how warranted this is TBH.
We'd need stronger arguments for it I think.
I'll collate the arguments as well :)
If the "sed" method works reliably, it leaves error checking: how to reliably
check for error in such a pipe (inside a posix shell script)?
The closest code I found is this: https://github.com/cheusov/pipestatus which
seems very long.
For completeness, showing the current options for such cases...
So additional arguments are:
1. robust error checking
2. simplicity of use: if 'sort' had this option built-in, the following use cases would
"just work". with sed+sort, it will require different invocations (and probably
different pitfalls):
a. one input file
(sed -u 1q && sort) < file
b. one input pipe
seq 10 | ( sed -u 1q && sort -n )
c. multiple input files (without resorting to pipe, as this will cause
'sort' to use different amount of memory)
So for multiple files, we'd only take the header from the first, I suppose:
(head -q -n1 file.* | head -n1; tail -q -n+2 file.* | sort)
There is also the --merge case.
This is especially awkward with the per file constructs:
(head -q -n1 file.*; sort -m <(tail -n+2 file.1) <(tail -n+2 file.1))