如何在没有线条的情况下对列进行排序?

时间:2016-10-11 12:25:38

标签: bash shell

有人可以解释我该怎么做? 我有一个文件,有三列,和很多行,我希望第二列按升序排序(它只包含数字)。

sample file; 

7.31937  736    /tmp/ref13
7.3223   5373   /tmp/ref13
7.32816  768    /tmp/ref13
7.32955  5370   /tmp/ref10

我想;

7.31937  736    /tmp/ref13
7.3223   768    /tmp/ref13
7.32816  5370   /tmp/ref13
7.32955  5373   /tmp/ref10

谢谢!

1 个答案:

答案 0 :(得分:0)

你可以试试这个;

paste -d' '  <(awk '{print $1}' yourFile) <(awk '{print $2}' yourFile | sort -n) <(awk '{print $3}' yourFile)

awk '{print $2}' yourFile | sort -n | paste yourFile - | awk '{print $1"\t"$4"\t"$3}'

例如:

user@host:/tmp$ cat t1
7.31937  736    /tmp/ref13
7.3223   5373   /tmp/ref13
7.32816  768    /tmp/ref13
7.32955  5370   /tmp/ref10

user@host:/tmp$ paste -d' '  <(awk '{print $1}' t1) <(awk '{print $2}' t1 | sort -n) <(awk '{print $3}' t1)  | column -t
7.31937  736   /tmp/ref13
7.3223   768   /tmp/ref13
7.32816  5370  /tmp/ref13
7.32955  5373  /tmp/ref10