I want a command sorth
(=sort
with h
eader) that is like the unix sort
command with all the command-line options, but also able to avoid headers. I tried the following ways:
sorth
(attempt 1):
#!/opt/local/bin/bash
head -n 1 $1 && $1 -n +2 table | sort
The problem with this is 1) that I don't know how to distinguish between the target file $1
and any options in the sort command at the end of the 2nd line, and (2) it seems inefficient to access the file twice before you even get to the sort command.
Then I tried (using this, modifying @Dave's answer to allow command line options)
sorth
(attempt 2):
#!/opt/local/bin/bash
gawk -v options="$1" '
BEGIN {FS=",";OFS=","}
NR==1 {print;next}
{print| "sort $options" }'
But this doesn't work either, probably also because I haven't distinguished between the sort
options and the target file.
I want the command to be flexible to use a target file or piped STDIN.
How can I do this?
What would be even cooler is to include a new option --retain-headers
under the existing sort
command. But I suspect that's way hard and dangerous.