I want to order some command output (using pipeline), taking into account some field from the output.
For example, if I run l
command, I have:
-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
I want to order this output according for example by the day of the month field, so the output should be:
-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
How can I do this? I usually use grep
, cat
, l
, ls
, ll
, but I can't figure out how to achieve this.
You can use sort
with the 7th column:
$ 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
From man sort
:
-n, --numeric-sort
compare according to string numerical value
-k, --key=KEYDEF
sort via a key; KEYDEF gives location and type
However, this is quite fragile and, in general, you should not parse the output of ls
.