The following awk script processes:
- any matrix size
- there is no relationship between row and column indices, so it tracks them separately.
- If the column index of a specific column is not displayed, the default value will be zero.
This is done as follows:
awk ' BEGIN{PROCINFO["sorted_in"] = "@ind_num_asc"} (NR==1){next} {row[$1]=1;col[$2]=1;val[$1" "$2]=$3} END { printf "%8s",""; for (j in col) { printf "%8.3f",j }; printf "\n" for (i in row) { printf "%8.3f",i; for (j in col) { printf "%8.3f",val[i" "j] }; printf "\n" } }' <file>
How it works:
PROCINFO["sorted_in"] = "@ind_num_asc" , states that all arrays are sorted numerically by index.(NR==1){next} : skip the first line{row[$1]=1;col[$2]=1;val[$1" "$2]=$3} , process the row while keeping the row and column index and associated value.- The end statement does the whole print.
It is output:
20.500 21.500 22.500 20.500 -4.100 1.200 7.000 21.500 -6.200 4.300 10.400 22.500 0.000 6.000 16.700
Note: using PROCINFO is a gawk feature.
However, if you make a couple of assumptions, you can make it much shorter:
- file contains all possible entries, missing values
- You do not want to display row and column indices:
- indexes are sorted into column-major-order
You can use the following short versions:
sort -g <file> | awk '($1+0!=$1){next} ($1!=o)&&(NR!=1){printf "\n"} {printf "%8.3f",$3; o=$1 }'
which outputs
-4.100 1.200 7.000 -6.200 4.300 10.400 0.000 6.000 16.700
or for transposed:
awk '(NR==1){next} ($2!=o)&&(NR!=2){printf "\n"} {printf "%8.3f",$3; o=$2 }' <file>
Displays
-4.100 -6.200 0.000 1.200 4.300 6.000 7.000 10.400 16.700
kvantour
source share