How to sort matrix table in UNIX?

Hello All,
i have a file sort.txt with below entries.
1 12 10
16 6 4
20 8 15

i need to sort these entries and the out put should come in a single line.

1 4 6 8 10 12 15 16 20

Can you please help me sort this out?

Would you consider:-

cat sort.txt | tr " " " ^J" |sort -n | while read line
do
   print "$line \c"
done;print

Not pretty, but it seems to work. The ^J bit is a single character, usually generated by the key sequence <CNTRL>v then ENTER.

What this does is to break each line up to separate lines using a space as a delimiter. It then sorts then numerically and then connects them all back together on a single output line using the \c to avoid splitting the line and finishing with an extra print command to ensure you get the output complete with a new-line.

There may well be prettier or more efficient ways to do this. How big is your file?

I hope that this helps.
Robin
Liverpool/Blackburn
UK

An approach using gawk and asort sorting function:

gawk ' {
        for(i=1; i<=NF; i++)
                a[++j] = $i
} END {
        n = asort(a)
        for(i=1; i<=n; i++)
                printf "%d ", a
} ' sort.txt