我想命令一些命令输出(使用管道),同时考虑输出中的一些字段。
例如,如果我运行 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
,但是我无法弄清楚如何实现这一目标。
最佳答案
您可以对第 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/