按文件大小在unix中排序 [英] sorting in unix by file size

查看:34
本文介绍了按文件大小在unix中排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含如下数据的 unix 文件:

I have a unix file with data like:

35|ag
0|ca
22.0 K|nt
43.8 G|ct
90.0 M|se
2.4 M|ew
1.6 K|et
0|er
0|dr
18|ld

输出:

43.8 G|ct   
90.0 M|se
2.4 M|ew    
22.0 K|nt
1.6 K|et
35|ag
18|ld
0|ca
0|er
0|dr

我需要按大小递减顺序对其进行排序.我可以将其转换为统一大小,例如字节,然后对它们进行排序,但我希望它是否可以直接排序.

I need to sort this in decreasing order of size. I could convert this to uniform size such as bytes and then sort them, but I was hoping if it could be sorted directly.

提前致谢!

推荐答案

使用 GNU 排序.man sort:

Using GNU sort. man sort:

   -h, --human-numeric-sort
          compare human readable numbers (e.g., 2K 1G)

但我们需要从第一个字段中删除空格.使用 tr

but we need to remove the spaces from the first field. Using tr

$ cat file | tr -d ' ' | sort -h -r -t\| -k 1
43.8G|ct
90.0M|se
2.4M|ew
22.0K|nt
1.6K|et
35|ag
18|ld
0|er
0|dr
0|ca

这篇关于按文件大小在unix中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆