Sorting Row and Column from Table of Number

I’m not sure whether someone already ask about this or not, if someone already did forgive me.

I have a chunk of random number (my_number)

1 2 5 8 3 4

3 7 5 4 7 2

2 3 7 7 9 1

I want to sort them both by row and column:

by row

1 2 3 4 5 8

2 3 4 5 7 7

1 2 3 7 7 9 

by column

1 2 5 4 3 1

2 3 5 7 7 2

3 7 7 8 9 4

So far, I’ve tried

while read line; do
tr, $'n' < <(printf -- "%s" "$line") | sort -g | tr $ 'n', | sed 's/,$/n/';
done < my_number

and I also tried some basic command like

sort -g my_number

sort -n my_number

However, apparently every one of them only sort the first column, the rest still randomly scattered.

Is my idea about sorting both by row then change by column even possible? A help in fixing my code or new code will be highly appreciated. Thanks


Source: bash

Leave a Reply