2015-11-03 84 views
0

我想要命令一些命令輸出(使用管道),並考慮輸出中的某些字段。使用行字段命令輸出命令

例如,如果我跑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,但我不知道如何實現這一點。

+2

'ls'可以做一些整理自身,而不是特定的(奇怪)之一。但是,[不要解析'ls'!](http://mywiki.wooledge.org/ParsingLs) – Biffen

回答

2

您可以使用sort與第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 

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

使用ls -tls -tr

可以google一下

+0

不,這不會顯示OP想要的輸出。 '-t'根據時間戳排序,所以5 oct將在6 nov之前,但在6 sept之後。 – fedorqui