linux - 使用行字段排序命令输出

标签 linux sorting command-line terminal pipeline

我想命令一些命令输出(使用管道),同时考虑输出中的一些字段。

例如,如果我运行 l 命令,我有:

-rw-r-----  1 matias matias  67843408 sep 11 08:55 file1
-rw-r-----  1 matias matias      1952 oct 23 12:05 file2
-rw-r-----  1 matias matias       965 oct 23 10:14 asd.txt
-rw-r-----  1 matias matias    892743 sep  3 08:36 aaa.txt
-rw-r-----  1 matias matias    892743 ago 18 08:09 qwe

我想根据日期字段对输出进行排序,因此输出应该是:

-rw-r-----  1 matias matias    892743 sep  3 08:36 aaa.txt
-rw-r-----  1 matias matias  67843408 sep 11 08:55 file1
-rw-r-----  1 matias matias    892743 ago 18 08:09 qwe
-rw-r-----  1 matias matias      1952 oct 23 12:05 file2
-rw-r-----  1 matias matias       965 oct 23 10:14 asd.txt

我该怎么做?我通常使用grepcatllsll,但是我无法弄清楚如何实现这一目标。

最佳答案

您可以对第 7 列使用排序:

$ sort -k7 -n file
-rw-r-----  1 matias matias    892743 sep  3 08:36 aaa.txt
-rw-r-----  1 matias matias  67843408 sep 11 08:55 file1
-rw-r-----  1 matias matias    892743 ago 18 08:09 qwe
-rw-r-----  1 matias matias      1952 oct 23 12:05 file2
-rw-r-----  1 matias matias       965 oct 23 10:14 asd.txt

来自人工排序:

  -n, --numeric-sort
          compare according to string numerical value

   -k, --key=KEYDEF
          sort via a key; KEYDEF gives location and type

然而,这是非常脆弱的,一般来说,you should not parse the output of ls .

关于linux - 使用行字段排序命令输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33501217/

相关文章:

linux - 在 linux 64 位下链接 32 位库

php - 在linux命令行上使用python(未运行)编译字符串

sorting - 为什么反转的分布在插入排序中不重要?

javascript - 将新列表项插入其正确的字母顺序位置

python - 使用python返回参数到命令行

ruby - 系统命令不断失败,退出代码为 127

c++ - 在拖动/调整窗口大小时在窗口上调用 isActive() 时,返回 false。 (X11, Qt 5.4.0)

linux - 查找使用我的 rpm 的模块

java - 用随机数填充数组时 Math.random 的非常非随机因子

command-line - 通过 redis-cli 检查并重试 resque 作业