【发布时间】:2011-10-26 16:55:18
【问题描述】:
我有一个问题,我设法通过变通解决了,所以我在这里希望向你学习更优雅的解决方案;-)
我必须解析一个程序的输出:它会像这样写一个包含三列 x y z 的文件
1 1 11
1 2 12
1 3 13
1 4 14
2 1 21
2 2 22
2 3 23
2 4 24
3 1 31
3 2 32
3 3 33
3 4 34
4 1 41
4 2 42
4 3 43
4 4 44
在这样的矩阵中
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
我用这样的两行 bash 脚本解决了
dim_matrix=$(awk 'END{print sqrt(NR)}' file_xyz) #since I know that the matrix has to be squared and there are no blank lines in the file_xyz
awk '{printf("%s%s",$3, !(NR%'${dim_matrix}'==0) ? OFS :ORS ) }' file_xyz
您能否建议我一种仅使用 awk 执行相同操作的方法?
【问题讨论】: