我有一个类似于以下示例的文件格式,该文件格式显示了5个人(包括他们自己)之间的关系.
I have a file format like the example below showing the relationships between 5 individuals including themselves.
1 1 1.0 2 1 0.5 3 1 0.1 4 1 0.3 5 1 0.1 2 2 1.0 3 2 0.5 4 2 0.2 5 2 0.3 3 3 1.0 4 3 0.5 5 3 0.3 4 4 1.0 5 4 0.1 5 5 1.0我想使用AWK将其转换为完整的矩阵格式.我将需要像示例中那样对行和列进行数字排序.
I would like to use AWK to convert it into a full matrix format. I would be necessary to have the rows and columns sorted numerically as in the example.
1 2 3 4 5 1 1.0 0.5 0.1 0.3 0.1 2 0.5 1.0 0.5 0.2 0.3 3 0.1 0.5 1.0 0.5 0.3 4 0.3 0.2 0.5 1.0 0.1 5 0.1 0.3 0.3 0.1 1.0我遇到了上一个线程(如下),但是输入文件的格式略有不同,我正在努力调整它. www.unix. com/shell-programming-and-scripting/203483-how-rearrange-matrix-awk.html
I came across a previous thread (below) but the format of the input file is slightly different and i am struggling to adjust it. www.unix/shell-programming-and-scripting/203483-how-rearrange-matrix-awk.html
如何执行此转换?
推荐答案在这里, gawk 解决方案:
Here we go, gawk solution:
matrixize.awk 脚本:
matrixize.awk script:
#!/bin/awk -f BEGIN { OFS="\t" } # output field separator { b[$1]; # accumulating unique indices if ($1 != $2) { a[$2][$1] = $3 # set `diagonal` relation between different indices } a[$1][$2] = $3 # multidimensional array (reflects relation `one-to-many`) } END { asorti(b); h = ""; # sort unique indices for (i in b) { h = h OFS i # form header columns } print h; # print header column values for (i in b) { row = i; # index column # iterating through the row values (for each intersection point) for (j in a[i]) { row = row OFS a[i][j] } print row } }用法 :
Usage:
awk -f matrixize.awk yourfile输出:
1 2 3 4 5 1 1.0 0.5 0.1 0.3 0.1 2 0.5 1.0 0.5 0.2 0.3 3 0.1 0.5 1.0 0.5 0.3 4 0.3 0.2 0.5 1.0 0.1 5 0.1 0.3 0.3 0.1 1.0更多推荐
将3列文件转换为矩阵格式
发布评论