To sort dates in a Linux or Unix shell, you can use the sort
command with the -k
option and a field specification to specify which field to sort on.
For example, to sort a list of dates in the YYYY-MM-DD
format, you can use the following command:
sort -k1,4n -k2n -k3nSource:www.lautturi.com
This will sort the dates in ascending order, with the oldest dates appearing first.
The -k1,4n
option tells sort
to sort the first field (the year) numerically. The -k2n
option tells sort
to sort the second field (the month) numerically. The -k3n
option tells sort
to sort the third field (the day) numerically.
You can also use the -r
option to sort the dates in descending order, with the newest dates appearing first. For example:
sort -r -k1,4n -k2n -k3n
If you want to sort dates in a different format, such as DD/MM/YYYY
, you can use a different field specification. For example:
sort -k3n -k2n -k1,4n
This will sort the dates in ascending order, with the oldest dates appearing first.
Keep in mind that the sort
command only works with plain text files. If you want to sort dates in other types of files, such as CSV or Excel files, you will need to use a different tool or script.