Numerical sorting of every column in a file individually using awk
You can do it in a single GNU awk:
gawk -F ',' '
{
for(i=1;i<=NF;i++){matrix[i][NR]=$i}
}
END{
for(i=1;i<=NF;i++){asort(matrix[i])}
for(j=1;j<=NR;j++){
for(i=1;i<NF;i++){
printf "%s,",matrix[i][j]
}
print matrix[i][j]
}
}
' file
for(i=1;i<=NF;i++){matrix[i][NR]=$i}
Multidimensional array (GNU extension) matrix
gets populated, so that matrix[i][j]
contains the number of column i
, row j
.
for(i=1;i<=NF;i++){asort(matrix[i])}
Sorts each column (GNU extension).
Finally
for(j=1;j<=NR;j++){ for(i=1;i<NF;i++){ printf "%s,",matrix[i][j] } print matrix[i][j] }
Prints a sequence of a[1],
, a[2],
, ..., a[NF-1],
, a[NF]\n
for each line.