sort at jobs chronologically

Assuming you're on Linux, the output of atq always has the date in the same format. Sort the fields in the appropriate order, taking care to declare which ones are numbers or month names. Make sure to use an English locale for the month names since that's what atq uses.

atq | sort -k 6n -k 3M -k 4n -k 5 -k 7 -k 1
#          year  month day   time queue id

The sort command can do it, but unfortunately, you can't use --month-sort and --numeric-sort together. So use:

$ atq |
    sed 's/Jan/1/;s/Feb/2/;s/Mar/3/;s/Apr/4/;s/May/5/;s/Jun/6/;s/Jul/7/;s/Aug/8/;s/Sep/9/;s/Oct/10/;s/Nov/11/;s/Dec/12/' |
    sort -n -k6,6 -k3,4

This will convert the month abbreviations to their numeric values, then sort first on year (-k6,6), then month and day (-k3,4). The output won't have the month names, but if you really wanted, you could convert those back with another sed.

$ atq |
    sed 's/Jan/1/;s/Feb/2/;s/Mar/3/;s/Apr/4/;s/May/5/;s/Jun/6/;s/Jul/7/;s/Aug/8/;s/Sep/9/;s/Oct/10/;s/Nov/11/;s/Dec/12/' |
    sort -n -k6,6 -k3,4 |
    sed 'h;s/^[0-9][0-9]*  *[A-Z][a-z][a-z] *\([0-9][0-9]*\).*/\1/;s/10/Oct/;s/11/Nov/;s/12/Dec/;s/1/Jan/;s/2/Feb/;s/3/Mar/;s/4/Apr/;s/5/May/;s/6/Jun/;s/7/Jul/;s/8/Aug/;s/9/Sep/;G;s/^\(.*\)\n\([0-9][0-9]*  *[A-Z][a-z][a-z] *\)[0-9][0-9]*\( .*\)/\2\1\3/'

Notice that s/12/Dec/ needs to come before s/1/Jan/.

Tags:

Date

Sort

At