bash - Sorting Row and Column from Table of Number -


i'm not sure whether ask or not, if did forgive me.

i have 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 sort them both row , 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; tr, $'\n' < <(printf -- "%s" "$line") | sort -g | tr $ '\n', | sed 's/,$/\n/'; done < my_number 

and tried basic command like

sort -g my_number  sort -n my_number 

however, apparently every 1 of them sort first column, rest still randomly scattered.

is idea sorting both row change column possible? in fixing code or new code highly appreciated. thanks

going from:

1 2 5 8 3 4  3 7 5 4 7 2  2 3 7 7 9 1 

to this:

1 2 3 4 5 8  2 3 4 5 7 7  1 2 3 7 7 9  

is not terribly difficult, correcting original attempt:

while read line;     tr ' ' $'\n' <<< $line | sort -g | tr '\n' , | sed 's/,/ /g' done < the_file 

the above not terribly difficult because input can processed row row.

sorting values in each column harder. 1 way it:

  1. transpose values
  2. apply sorting logic row (first part of question)
  3. transpose values

one way implement transpose:

transpose() {     gawk '{ (i = 1; <= nf; ++i) a[i] = a[i] " " $i } end {for (i in a) { print substr(a[i], 2); print "" }}' } 

for clarity, let's put sorting logic in function too:

sort_each_row() {     while read line;         tr ' ' $'\n' <<< $line | sort -g | tr '\n' , | sed 's/,/ /g'     done } 

and can do:

transpose < the_file | sort_by_row | transpose 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -