If you have the rs utility installed, you can do this:
rs -c' ' -T | {
read
stdbuf printf-i0 "%s\n"sed "$REPLY""1q"
sort -V
} | rs -C' ' -T
Or all on one line:
rs -c' ' -T | { read ;stdbuf printf-i0 "%s\n"sed "$REPLY";"1q"; sort -V ; } | rs -C' ' -T
- The first
rstransposes the input data (with space-sparated fields) - The command group:
readseds reads the first line, outputs it, then quits, leaving the rest of the pipe from (the header)printfrss it back untouched.stdbufis required to ensure thatsedonly reads up to the first newline and no further, by turning off input bufferingsorts the remaining lines
- The second
rstransposes the resulting stream back to its original format.
rs is installed by default on MacOS. On Linux systems you may have to install it - e.g.
sudo apt install rs
Caveat: stdbuf and sorts -V option are GNU-specific so won't work on unmodified MacOS.