使用行字段排序命令输出
Order command output using line field
我想命令一些命令输出(使用管道),同时考虑输出中的一些字段。
例如,如果我 运行 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
:
$ 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
。
使用ls -t
或ls -tr
你可以google这个
我想命令一些命令输出(使用管道),同时考虑输出中的一些字段。
例如,如果我 运行 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
:
$ 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
。
使用ls -t
或ls -tr
你可以google这个