我有这样一个文件(数百行和列)。
1 2 34 5 67 9 88和我想重新排列基础上最后一行值的列(或特定行的值)
1 3 24 6 57 9 88我如何使用AWK(或其他方式)来完成这个任务?预先感谢您的帮助。
修改:我想感谢大家,并道歉,如果我不太清楚。我想这样做的是:
- 走线(例如最后一个);
- 使用所选行的排序的值到derermine顺序重新排序矩阵中的列。
所以,最后一行是 7 88 9 ,它分类为 7 9 88 ,那么三列已到的方式,使得在此情况下,最后两列互换进行重新排序
一个四柱更通用的例子的基础上,最后一行一次:
输入:
1 2 3 44 5 6 77 88.0 9 -3输出:
4 1 3 2 7 4 6 5-3 7 9 88.0解决方案
下面是一个简单的,肮脏的和可改进的解决方案:(编辑,因为OP澄清,数字浮点)
$猫TEST.DAT1 2 34 5 60.07 0.88 -.09$ awk的{$打印(printf的'$%D%.0s \\ n'\\ $(i = 0;在$ X(尾-n1 TEST.DAT); DO 回声$((++ i))的$ X 做| 排序-k2g)|粘贴-sd,)}TEST.DAT3 1 26 4 5-.09 0.07 0.88要看看会发生什么在那里(或至少它的一部分):
$回声{$打印(printf的'$%D%.0s \\ n'\\ $(i = 0;在$ X(尾-n1 TEST.DAT); DO 回声$((++ i))的$ X 做| 排序-k2g)|粘贴-sd,)}TEST.DAT{打印$ 3,$ 1,$ 2} TEST.DAT要使它成为一个独断专行的工作,取代尾-n1 与尾-n + $ L |头-n1
I have a file like this (with hundreds of lines and columns)
1 2 3 4 5 6 7 88 9and I would like to re-order columns basing on the last line values (or a specific line values)
1 3 2 4 6 5 7 9 88How can I use awk (or other) to accomplish this task? Thank you in advance for your help
EDIT: I would like to thank everybody and to apologize if I wasn't enough clear. What I would like to do is:
- take a line (for example the last one);
- reorder the columns of the matrix using the sorted values of the chosen line to derermine the order.
So, the last line is 7 88 9, which sorted is 7 9 88, then the three columns have to be reordered in a way such that, in this case, the last two columns are swapped.
A four-column more generic example, based on the last line again:
Input:
1 2 3 4 4 5 6 7 7 88.0 9 -3Output:
4 1 3 2 7 4 6 5 -3 7 9 88.0解决方案
Here's a quick, dirty and improvable solution: (edited because OP clarified that numbers are floating point).
$ cat test.dat 1 2 3 4 5 6 .07 .88 -.09 $ awk "{print $(printf '$%d%.0s\n' \ $(i=0; for x in $(tail -n1 test.dat); do echo $((++i)) $x done | sort -k2g) | paste -sd,)}" test.dat 3 1 2 6 4 5 -.09 .07 .88To see what's going on there (or at least part of it):
$ echo "{print $(printf '$%d%.0s\n' \ $(i=0; for x in $(tail -n1 test.dat); do echo $((++i)) $x done | sort -k2g) | paste -sd,)}" test.dat {print $3,$1,$2} test.datTo make it work for an arbitrary line, replace tail -n1 with tail -n+$L|head -n1
更多推荐
我怎样才能用awk通过列的最后一个值的列进行排序?
发布评论