我想订购一些命令输出(使用管道),考虑输出中的一些字段。
例如,如果我运行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
我该怎么做?我通常使用grep
,cat
,l
,ls
,ll
,但我无法弄清楚如何实现这一目标。
答案 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 -t
或ls -tr
你可以谷歌这个