Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Possibly a simple solution I misplaced. How can I get the output of atq sorted chronologically, so I can easily see who's due to run next? The man page for sort doesn't have anything built-in to recognize timestamps like the following:

atq
1264    Sat Mar 24 15:03:00 2012 a master
1445    Sat Mar 24 20:28:00 2012 a master
1548    Sun Mar 25 15:09:00 2012 a master
1193    Sat Mar 24 11:03:00 2012 a master
1359    Sat Mar 24 17:13:00 2012 a master
1726    Mon Mar 26 21:24:00 2012 a master
1736    Mon Mar 26 22:04:00 2012 a master
1748    Mon Mar 26 22:46:00 2012 a master
1704    Mon Mar 26 20:19:00 2012 a master
1288    Sat Mar 24 15:38:00 2012 a master
1532    Sun Mar 25 11:53:00 2012 a master

atq |sort wouldn't work either at the job id's jump around.

share|improve this question
    
I'm okay with reformatting the 2nd field as 2012-03-23_13-11-01 which is sorting-friendly, which I think date can do. With the --date=STRING param and +\%Y-\%m-\%d_\%H-\%M-\%S –  Marcos Mar 23 '12 at 12:14
add comment

3 Answers 3

up vote 8 down vote accepted

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
share|improve this answer
add comment

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/.

share|improve this answer
1  
On one host with 200+ jobs the output seems to be sorted by id mainly--not always timestamp in the 2nd half. Anyway, the date command has special timestamp-parsing powers that could make this simpler when fed the right fields eg. with cut. –  Marcos Mar 23 '12 at 12:07
1  
You can pass a separate sort option on each -k option: -k 3M and so on. –  Gilles Mar 23 '12 at 19:14
add comment

Looks complicated but this also works:

atq |awk '{system("echo "$1 "  $(date +%Y-%m-%d_%H-%M-%S \
--date \""$2" "$3" "$4" "$5" "$6"\")  "$7"  "$8 )}' |sort -k2 
    469  2012-03-24_01-30-00  a  master
    655  2012-03-24_02-03-00  a  master
    671  2012-03-24_02-04-00  a  master
    657  2012-03-24_02-09-00  a  master
    673  2012-03-24_02-11-00  a  master
    537  2012-03-25_00-38-00  a  master
    539  2012-03-25_00-43-00  a  master
    652  2012-03-27_12-57-00  a  master
    654  2012-03-27_13-03-00  a  master
    656  2012-03-27_13-09-00  a  master

Compared with other suggestions I like how the actual parsing is left to date which understands the textual timestamp, so you can reformat to whatever you want that makes it a breeze to filter or sort later.

You can tack on a |column -t at the end to nicely align and space out the fields.

share|improve this answer
    
Actually, a shorter version may be doable with bash readline instead of awk –  Marcos Mar 24 '12 at 0:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.