使用awk将列转换为矩阵格式 [英] Convert column to matrix format using awk
问题描述
我有一个列格式的网格数据文件,如下:
I have a gridded data file in column format as:
ifile.txt
x y value
20.5 20.5 -4.1
21.5 20.5 -6.2
22.5 20.5 0.0
20.5 21.5 1.2
21.5 21.5 4.3
22.5 21.5 6.0
20.5 22.5 7.0
21.5 22.5 10.4
22.5 22.5 16.7
我想将其转换为矩阵格式,如下:
I would like to convert it to matrix format as:
ofile.txt
20.5 21.5 22.5
20.5 -4.1 1.2 7.0
21.5 -6.2 4.3 10.4
22.5 0.0 6.0 16.7
顶部的20.5 21.5 22.5
表示y,侧面的值表示x,内部的值表示对应的网格值.
Where top 20.5 21.5 22.5
indicate y and side values indicate x and the inside values indicate the corresponding grid values.
我在这里找到了类似的问题将3列文件转换为矩阵格式,但该脚本在我的情况下不起作用.
I found a similar question here Convert a 3 column file to matrix format but the script is not working in my case.
脚本是
awk '{ h[$1,$2] = h[$2,$1] = $3 }
END {
for(i=1; i<=$1; i++) {
for(j=1; j<=$2; j++)
printf h[i,j] OFS
printf "\n"
}
}' ifile
推荐答案
以下awk
脚本句柄:
- 任何大小的矩阵
- 行索引和列索引之间没有关系,因此可以分别跟踪它们.
- 如果未出现某个行列索引,则该值将默认为零.
这是通过以下方式完成的:
This is done in this way:
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>
它是如何工作的:
-
PROCINFO["sorted_in"] = "@ind_num_asc"
,指出所有数组均按索引按数字排序. -
(NR==1){next}
:跳过第一行 -
{row[$1]=1;col[$2]=1;val[$1" "$2]=$3}
,通过存储行和列索引以及随附的值来处理行. - end语句完成所有打印.
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 line by storing the row and column index and accompanying value.- The end statement does all the printing.
这将输出:
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
注意:PROCINFO
的使用是gawk
功能.
但是,如果您做几个假设,则可以做得更短:
However, if you make a couple of assumptions, you can do it much shorter:
- 该文件包含所有可能的条目,没有丢失的值
- 您不希望打印出行和列的索引:
- 索引按列主要顺序 进行排序>
- the file contains all possible entries, no missing values
- you do not want the indices of the rows and columns printed out:
- the indices are sorted in column-major-order
您可以使用以下简短版本:
The 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 }'
输出
-4.100 1.200 7.000
-6.200 4.300 10.400
0.000 6.000 16.700
或换位:
awk '(NR==1){next}
($2!=o)&&(NR!=2){printf "\n"}
{printf "%8.3f",$3; o=$2 }' <file>
此输出
-4.100 -6.200 0.000
1.200 4.300 6.000
7.000 10.400 16.700
这篇关于使用awk将列转换为矩阵格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!