bash 按整数值排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16129403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting by integer value
提问by user1926550
I am writing a bash script and I am using
我正在编写一个 bash 脚本并且我正在使用
ps -e -o %cpu
ps -e -o %cpu
command.
命令。
I would like to have output of sorted %cpuvalues (descending). How to do that?
I know that I should use sort command but I don't know how.
我想输出排序%cpu值(降序)。怎么做?我知道我应该使用 sort 命令,但我不知道如何使用。
回答by perreal
ps -e -o %cpu | sort -nr
nfor numeric, rfor reverse. If you also want to remove the header:
n用于数字,r用于反向。如果您还想删除标题:
ps -e -o %cpu | sed '1d' | sort -nr
回答by n3rV3
ps has an inbuilt option that sorts its output, based on any field of choice. You can use
ps 有一个内置选项,可以根据选择的任何字段对其输出进行排序。您可以使用
ps k -%cpu -e -o %cpu
Here, ksorts the output based on field provided and -%cpuis to sort it in descending order.
在这里,k根据提供的字段对输出-%cpu进行排序,并按降序对其进行排序。
If you omit the -in front of the sort field then it will be sorted in ascending order.
Also note that you can give it multiple sort fields:
如果省略-排序字段前面的 ,则它将按升序排序。另请注意,您可以为其提供多个排序字段:
ps k -%cpu,-%mem -e -o %cpu,%mem
This sorts the output(in descending order for both) first based on the %cpufield and second based on %memfield.
这首先根据%cpu字段对输出(按降序排列)进行排序,然后根据%mem字段对输出进行排序。

