Considering the following input and output:
infile | outfile
1 3 5 2 4 | 1 2 3 4 5
2 4 5 | 2 4 5
4 6 2 1 | 1 2 4 6
Is there any combination of UNIX programs, not involving programming languages -- not any other than the shell scripting itself --, that sorts the entries in each line of a file faster than the following approach:
while read line; do
tr ' ' '\n' <<< ${line} | sort | tr '\n' ' '
echo ""
done < infile > outfile
I mean, I'm able to create a small cpp/python/awk/... program to do so, but it is just not the same as using the usual one-liners to magically solve problems.
Edit:
I must have added too much text, instead of simply asking what I wanted; straightforwardly, I wanted to confirm whether there was any UNIX program/combination of programs (using pipes, fors, whiles, ...) capable of sorting entries in a line, but without as much overhead as the one solution above.
I know I may do the nasty job in a programming language, like perl, awk, python, but I was actually looking for a composition of UNIX programs that wouldn't involve these language interpreters. From the answers, I must conclude there is no such inline sort tool(s), and I'm very thankful for the solutions I've got -- mainly the very neat Perl one-liner.
Yet, I do not really understand the reason for so much overhead on the Bash approach I posted. Is it really due to a multitude of context switches, or is it simply the overhead of translating back and fro the input, and sorting it?
I can't seem to understand which of these steps is slowing down the execution so much. It takes several minutes to sort the entries in a file with ~500k lines, with ~30 values in each line.
sort -nif you need numeric sort.