Skip to main content
added 151 characters in body
Source Link
jimmij
  • 48.7k
  • 20
  • 136
  • 141

If you have GNU awk, then sorting can be done with a little help from special array PROCINFO:

awk -F '[. ]' '{for(i=1;i<NF+1;i++) a[i][NR]=$i} \
    END{PROCINFO["sorted_in"]="@val_num_asc"; \
        for(j=1;j<NF+1;j++){ I=0; for(i in a[j]) A[++I][j]=a[j][i]} \
            for(i=1;i<NR+1;i++){ printf A[i][1]"."; \
                for(j=2;j<NF+1;j++) printf A[i][j]" "; printf "\n"}}' file

The above may look complicated at first, but in fact is very simply - it just store the whole file in the array a, and at the end resort it to array A as desired. The main trick is to use @val_num_asc as we want to sort the columns by values in numeral ascending order.

It is should work for any number of rows and columns, just keep in mind that the whole file is stored in the memory so can be slow for large tables.

If you have GNU awk, then sorting can be done with a little help from special array PROCINFO:

awk -F '[. ]' '{for(i=1;i<NF+1;i++) a[i][NR]=$i} \
    END{PROCINFO["sorted_in"]="@val_num_asc"; \
        for(j=1;j<NF+1;j++){ I=0; for(i in a[j]) A[++I][j]=a[j][i]} \
            for(i=1;i<NR+1;i++){ printf A[i][1]"."; \
                for(j=2;j<NF+1;j++) printf A[i][j]" "; printf "\n"}}' file

The above may look complicated at first, but in fact is very simply - it just store the whole file in the array a, and at the end resort it to array A as desired. The main trick is to use @val_num_asc as we want to sort the columns by values in numeral ascending order.

If you have GNU awk, then sorting can be done with a little help from special array PROCINFO:

awk -F '[. ]' '{for(i=1;i<NF+1;i++) a[i][NR]=$i} \
    END{PROCINFO["sorted_in"]="@val_num_asc"; \
        for(j=1;j<NF+1;j++){ I=0; for(i in a[j]) A[++I][j]=a[j][i]} \
            for(i=1;i<NR+1;i++){ printf A[i][1]"."; \
                for(j=2;j<NF+1;j++) printf A[i][j]" "; printf "\n"}}' file

The above may look complicated at first, but in fact is very simply - it just store the whole file in the array a, and at the end resort it to array A as desired. The main trick is to use @val_num_asc as we want to sort the columns by values in numeral ascending order.

It is should work for any number of rows and columns, just keep in mind that the whole file is stored in the memory so can be slow for large tables.

Source Link
jimmij
  • 48.7k
  • 20
  • 136
  • 141

If you have GNU awk, then sorting can be done with a little help from special array PROCINFO:

awk -F '[. ]' '{for(i=1;i<NF+1;i++) a[i][NR]=$i} \
    END{PROCINFO["sorted_in"]="@val_num_asc"; \
        for(j=1;j<NF+1;j++){ I=0; for(i in a[j]) A[++I][j]=a[j][i]} \
            for(i=1;i<NR+1;i++){ printf A[i][1]"."; \
                for(j=2;j<NF+1;j++) printf A[i][j]" "; printf "\n"}}' file

The above may look complicated at first, but in fact is very simply - it just store the whole file in the array a, and at the end resort it to array A as desired. The main trick is to use @val_num_asc as we want to sort the columns by values in numeral ascending order.