使用行字段命令输出命令

时间:2015-11-03 14:09:30

标签: 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,但我无法弄清楚如何实现这一目标。

2 个答案:

答案 0 :(得分:2)

您可以在第7列使用sort

$ 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

来自man sort

  -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

答案 1 :(得分:-1)

使用ls -tls -tr

你可以谷歌这个