使用Shell命令排序文件

时间:2013-09-06 22:23:07

标签: bash shell sorting sed awk

我的数据看起来像这样

$ cat file
(71149, 3079, 68070, 0.0433) Alex
(51135, 2881, 48254, 0.0563) Brandon
(27231, 7105, 20126, 0.2609) Chad
(8365, 3634, 4731, 0.4344) Daniel
(7490, 7346, 144, 0.9808) Eliz
(6841, 3917, 2924, 0.5726) Frank
(6740, 7393, -653, 1.0969) Gates
(5084, 500, 4584, 0.0983) Harry
(5044, 3913, 1131, 0.7758) Ian
(4760, 699, 4061, 0.1468) Jack
....

我想按括号中的最后一个元素对内容进行排序,如何在命令行中执行此操作。

$ cat file | ... magic ... 
(6740, 7393, -653, 1.0969) Gates
(7490, 7346, 144, 0.9808) Eliz
(5044, 3913, 1131, 0.7758) Ian
(6841, 3917, 2924, 0.5726) Frank
(8365, 3634, 4731, 0.4344) Daniel
(27231, 7105, 20126, 0.2609) Chad
(4760, 699, 4061, 0.1468) Jack
(5084, 500, 4584, 0.0983) Harry
(51135, 2881, 48254, 0.0563) Brandon
(71149, 3079, 68070, 0.0433) Alex
....

我成功地对数字进行了排序,但我不知道如何在awk中打​​印整行。

$cat file | sed 's/)//g' | awk 'BEGIN{FS=" "}{print $4}' | sort -r
5.1246
4.3936
2.7811
2.5
1.9
....

1 个答案:

答案 0 :(得分:5)

kent$  sort  -k4nr file
(6740, 7393, -653, 1.0969) Gates
(7490, 7346, 144, 0.9808) Eliz
(5044, 3913, 1131, 0.7758) Ian
(6841, 3917, 2924, 0.5726) Frank
(8365, 3634, 4731, 0.4344) Daniel
(27231, 7105, 20126, 0.2609) Chad
(4760, 699, 4061, 0.1468) Jack
(5084, 500, 4584, 0.0983) Harry
(51135, 2881, 48254, 0.0563) Brandon
(71149, 3079, 68070, 0.0433) Alex