[Durham] interesting but expasperating problem sorting rows containing output from 'date' command [resend]

Dougie Nisbet dougie at highmoor.co.uk
Wed Aug 14 17:47:25 UTC 2013


I have an unimportant but irritating problem with sort. I have a file 
that contains information about hosts based on last 
successful/unsuccessful ping. Here's what it looks like (hostname 
anonymised):

10.220.133.101hostnamesince unknown
10.220.133.102hostnamesince Thu Jun 27 19:30:38 BST 2013      RIV
10.220.133.103hostnamesince unknown                   GMB
10.220.133.104hostnamesince Wed Feb 20 04:37:23 GMT 2013      REC
10.220.133.105hostnamesince Wed Feb 20 04:38:05 GMT 2013      1AD
10.220.133.106hostnamesince Fri Dec  7 14:38:04 GMT 2012      2BB
10.220.133.107hostnamesince Fri Dec  7 14:38:04 GMT 2012      3AA
10.220.133.108hostnamesince Tue Dec 11 15:00:04 GMT 2012      3AB
10.220.133.109hostnamesince Fri Dec  7 14:38:04 GMT 2012      NFM
10.220.133.254hostnamesince unknown
192.168.0.11hostnamesince Fri Jun  7 08:16:03 BST 2013      GMB  Gi1/0/24
192.168.0.12hostnamesince Mon Aug 12 16:45:40 BST 2013      GMB  Gi1/0/24
192.168.0.13hostnamesince Tue Jul 30 12:47:07 BST 2013      GMB  Gi1/0/24
192.168.0.14hostnamesince Fri Jun  7 08:16:06 BST 2013      GMB  Gi1/0/24
192.168.0.19hostnamesince unknown                   GMB  Gi1/0/24
192.168.0.20hostnamesince Fri Jul 19 23:06:10 BST 2013      GMB  Gi1/0/24
192.168.0.21hostnamesince unknown                   GMB  Gi1/0/23
192.168.0.22hostnamesince Wed Feb 20 04:42:31 GMT 2013      REC  Gi2/0/24
192.168.0.23hostnamesince Thu Jul 18 11:39:54 BST 2013      3AB  Gi1/0/24
192.168.0.25hostnamesince Wed Feb 20 04:42:58 GMT 2013      1AD  Gi1/0/24
192.168.0.27hostnamesince unknown                   RIV  Gi1/0/24
192.168.0.28hostnamesince Sun Jun 23 16:56:21 BST 2013      REC  Gi2/0/23
192.168.0.30hostnamesince Tue Jul  2 15:09:20 BST 2013      RIV  Gi1/0/3
192.168.0.31hostnamesince
192.168.0.32hostnamesince Tue Jul 23 12:20:58 BST 2013
192.168.0.33hostnamesince Tue Jul 23 12:21:00 BST 2013
192.168.0.34hostnamesince Tue Jul 23 12:21:01 BST 2013
192.168.0.201hostnamesince unknown                   RIV  Gi1/0/22
192.168.0.202hostnamesince unknown                   RIV  Gi1/0/24
192.168.0.203hostnamesince unknown                   RIV  Gi1/0/23
192.168.0.204hostnamesince unknown                   GMB  Gi1/0/2
192.168.0.205hostnamesince unknown                   GMB  Gi1/0/1
192.168.0.206hostnamesince unknown                   GMB  Gi1/0/4
192.168.0.207hostnamesince unknown                   REC  Gi1/0/23
192.168.0.208hostnamesince unknown                   1AD  Gi2/0/21
192.168.0.209hostnamesince unknown                   GMB  Gi1/0/3
192.168.0.210hostnamesince                   1AD  Gi2/0/23
192.168.0.211hostnamesince Thu Aug  1 09:54:58 BST 2013      REC  Gi1/0/24
192.168.0.212hostnamesince unknown                   1AD  Gi2/0/24
192.168.0.213hostnamesince unknown                   1AD  Gi2/0/22
192.168.0.214hostnamesince unknown                   REC  Gi1/0/21
192.168.0.215hostnamesince unknown                   2BB  Gi2/0/20
192.168.0.216hostnamesince unknown                   2BB  Gi2/0/19
192.168.0.217hostnamesince unknown                   REC  Gi1/0/22
192.168.0.218hostnamesince unknown                   3AA  Gi1/0/19
192.168.0.219hostnamesince unknown                   3AA  Gi1/0/23
192.168.0.220hostnamesince unknown                   REC  Gi1/0/20
192.168.0.221hostnamesince unknown                   3AB  Gi1/0/24
192.168.0.222hostnamesince unknown                   3AB  Gi1/0/23
192.168.0.223hostnamesince unknown                   3AB  Gi1/0/22
192.168.0.224hostnamesince unknown                   3AA  Gi1/0/24
192.168.0.225hostnamesince unknown                   3AB  Gi1/0/18
192.168.0.253hostnamesince unknown                   GMB  Gi1/0/24
192.168.1.1hostnamesince Tue Jun 25 15:07:38 BST 2013      2BB  Gi2/0/9
192.168.1.2hostnamesince Sat Jul 13 07:40:20 BST 2013      2BB  Gi1/0/2
192.168.1.3hostnamesince Sat Jul 13 07:38:12 BST 2013      2BB  Gi1/0/18
192.168.1.4hostnamesince Mon Aug 12 16:55:49 BST 2013      2BB  Gi1/0/4
192.168.1.5hostnamesince Mon Aug 12 17:23:09 BST 2013      2BB  Gi2/0/2
192.168.1.6hostnamesince unknown                   1AD  Gi1/0/1
192.168.1.7hostnamesince Mon Mar 11 15:28:51 GMT 2013      2BB  Gi1/0/19
192.168.1.8hostnamesince Fri Jul 12 03:38:09 BST 2013
192.168.1.10hostnamesince unknown                   EC   Gi1/0/6
192.168.1.11hostnamesince Mon Aug 12 17:23:19 BST 2013      2BB  Gi1/0/8
192.168.1.37hostnamesince Mon Aug 12 17:23:21 BST 2013      GMB  Gi1/0/21
192.168.1.40hostnamesince Wed Aug 14 04:37:46 BST 2013
192.168.1.42hostnamesince Mon Aug 12 17:23:24 BST 2013      GMB  Gi1/0/14
192.168.1.43hostnamesince Mon Aug 12 17:23:25 BST 2013      2BB  Gi1/0/5
192.168.1.45hostnamesince unknown                   GMB  Gi1/0/16
192.168.1.46hostnamesince unknown                   GMB  Gi1/0/16
192.168.1.101hostnamesince Mon Aug 12 17:23:43 BST 2013      2BB  Gi1/0/13
192.168.1.106hostnamesince Mon Aug 12 17:23:48 BST 2013      2BB  Gi1/0/11
192.168.1.111hostnamesince Mon Aug 12 17:23:49 BST 2013      2BB  Gi1/0/12
192.168.1.116hostnamesince unknown                   1AD  Gi2/0/14
192.168.1.122hostnamesince unknown                   2BB  Gi2/0/1
192.168.1.123hostnamesince unknown                   2BB  Gi1/0/7
192.168.1.124hostnamesince Mon Aug 12 17:23:54 BST 2013      GMB  Gi1/0/14
192.168.1.125hostnamesince Mon Aug 12 17:23:55 BST 2013      2BB  Gi2/0/12
192.168.1.126hostnamesince Mon Aug 12 17:23:57 BST 2013      RIV  Gi1/0/13
192.168.1.129hostnamesince Mon Aug 12 17:23:59 BST 2013      2BB  Gi1/0/5
192.168.1.130hostnamesince Mon Aug 12 17:24:00 BST 2013      GMB  Gi1/0/21
192.168.1.133hostnamesince Mon Aug 12 16:49:48 BST 2013      RIV  Gi1/0/1
192.168.1.135hostnamesince Mon Aug 12 17:24:07 BST 2013      RIV  Gi1/0/10
192.168.1.136hostnamesince Mon Aug 12 17:24:09 BST 2013      3AB  Gi1/0/1
192.168.1.137hostnamesince Mon Aug 12 16:49:56 BST 2013      3AB  Gi1/0/4
192.168.1.141hostnamesince Wed Jun 12 09:18:26 BST 2013
192.168.1.146hostnamesince Tue Jun 18 10:52:16 BST 2013
192.168.1.148hostnamesince unknown                   GMB  Gi1/0/16
192.168.1.151hostnamesince Tue May  7 09:14:06 BST 2013
192.168.1.152hostnamesince unknown
192.168.1.153hostnamesince unknown                   2BB  Gi1/0/17
192.168.1.154hostnamesince unknown                   EC   Gi1/0/4
192.168.1.155hostnamesince Thu Feb 14 10:57:04 GMT 2013
192.168.1.156hostnamesince Tue May 28 17:04:39 BST 2013      RIV  Gi1/0/4
192.168.1.157hostnamesince unknown                   RIV
192.168.1.158hostnamesince unknown                   RIV
192.168.1.159hostnamesince unknown                   RIV
192.168.1.160hostnamesince unknown                   RIV  Gi1/0/4
192.168.1.163hostnamesince unknown
192.168.1.164hostnamesince unknown                   EC   Gi1/0/1
192.168.1.165hostnamesince
192.168.1.166hostnamesince unknown                   EC   Gi1/0/1
192.168.1.167hostnamesince Fri Jul 19 16:28:13 BST 2013
192.168.1.188hostnamesince Mon Aug 12 16:50:22 BST 2013      2BB  Gi1/0/5
192.168.1.249hostnamesince unknown                   2BB  Gi2/0/8
192.168.1.254hostnamesince Mon Mar 11 15:11:30 GMT 2013
192.168.2.104hostnamesince Fri Jul 19 16:43:18 BST 2013
192.168.7.1hostnamesince Tue Jul 16 15:36:26 BST 2013
192.168.7.2hostnamesince Tue Jul 16 15:36:27 BST 2013
192.168.202.1hostnamesince unknown                   GMB  Gi1/0/20
192.168.202.2hostnamesince unknown                   2BB  Gi4/0/15
192.168.202.3hostnamesince unknown                   2BB  Gi4/0/4
192.168.202.4hostnamesince Wed Jul 24 15:58:54 BST 2013      2BB  Gi3/0/7
192.168.202.5hostnamesince unknown                   2BB  Gi4/0/6
192.168.202.6hostnamesince Mon Jul 22 16:22:13 BST 2013      2BB  Gi4/0/7
192.168.202.7hostnamesince unknown                   2BB  Gi4/0/8
192.168.202.8hostnamesince unknown                   2BB  Gi4/0/9
192.168.202.9hostnamesince unknown                   2BB  Gi3/0/3
192.168.202.10hostnamesince unknown                   2BB  Gi3/0/15
192.168.202.11hostnamesince unknown                   2BB  Gi4/0/10
192.168.202.12hostnamesince Thu Aug  1 14:53:02 BST 2013      2BB  Gi4/0/11
192.168.202.13hostnamesince unknown                   2BB  Gi4/0/12
192.168.202.14hostnamesince unknown                   2BB  Gi4/0/14
192.168.202.15hostnamesince unknown                   2BB  Gi3/0/4
192.168.202.16hostnamesince unknown                   2BB  Gi4/0/16
192.168.202.17hostnamesince unknown                   1AD  Gi2/0/11
192.168.202.18hostnamesince unknown                   1AD  Gi2/0/6
192.168.202.19hostnamesince unknown                   1AD  Gi2/0/5
192.168.202.21hostnamesince unknown                   1AD  Gi2/0/2
192.168.202.22hostnamesince unknown                   2BB  Gi4/0/19
192.168.202.23hostnamesince unknown                   2BB  Gi4/0/23
192.168.202.24hostnamesince                   1AD  Gi1/0/5
192.168.202.25hostnamesince unknown                   2BB  Gi4/0/20
192.168.202.26hostnamesince Mon Aug 12 16:51:03 BST 2013      REC  Gi1/0/3
192.168.202.27hostnamesince unknown                   2BB  Gi4/0/21
192.168.202.28hostnamesince unknown                   2BB  Gi1/0/5
192.168.202.29hostnamesince unknown                   1AD  Gi2/0/12
192.168.202.30hostnamesince unknown                   2BB  Gi3/0/14
192.168.202.31hostnamesince unknown                   1AD  Gi2/0/14
192.168.202.32hostnamesince unknown                   2BB  Gi4/0/24
192.168.202.33hostnamesince unknown
192.168.202.34hostnamesince unknown                   2BB  Gi3/0/21
192.168.202.35hostnamesince unknown                   2BB  Gi3/0/19
192.168.202.36hostnamesince unknown                   2BB  Gi3/0/17
192.168.202.37hostnamesince unknown                   2BB  Gi3/0/8
192.168.202.38hostnamesince unknown                   REC  Gi1/0/1
192.168.202.39hostnamesince unknown                   2BB  Gi4/0/2
192.168.202.40hostnamesince unknown                   1AD  Gi2/0/9
192.168.202.41hostnamesince unknown                   2BB  Gi1/0/20
192.168.202.42hostnamesince unknown                   1AD  Gi2/0/8
192.168.202.44hostnamesince unknown                   REC  Gi1/0/4
192.168.202.45hostnamesince unknown                   REC  Gi1/0/16
192.168.202.46hostnamesince unknown                   GMB  Gi1/0/16
192.168.202.48hostnamesince unknown                   2BB  Gi4/0/3
192.168.202.49hostnamesince unknown                   2BB  Gi3/0/13
192.168.202.50hostnamesince unknown                   GMB  Gi1/0/18
192.168.202.51hostnamesince unknown                   GMB  Gi1/0/19
192.168.202.52hostnamesince unknown                   2BB  Gi2/0/4
192.168.202.53hostnamesince unknown                   2BB  Gi3/0/11
192.168.202.54hostnamesince unknown                   2BB  Gi3/0/18
192.168.202.55hostnamesince unknown                   2BB  Gi4/0/13
192.168.202.56hostnamesince unknown
192.168.202.58hostnamesince
192.168.202.59hostnamesince unknown
192.168.202.60hostnamesince unknown                   2BB  Gi1/0/15
192.168.202.61hostnamesince unknown                   3AB  Gi1/0/10
192.168.202.62hostnamesince unknown                   2BB  Gi4/0/18
192.168.202.63hostnamesince unknown
192.168.202.64hostnamesince unknown                   2BB  Gi3/0/16
192.168.202.65hostnamesince unknown                   RIV  Gi1/0/19
192.168.202.66hostnamesince unknown                   RIV  Gi1/0/20
192.168.202.67hostnamesince unknown
192.168.202.68hostnamesince unknown                   2BB  Gi4/0/1
192.168.202.69hostnamesince unknown                   2BB  Gi4/0/5
192.168.202.70hostnamesince unknown                   RIV  Gi1/0/1
192.168.202.72hostnamesince unknown                   REC  Gi1/0/2
192.168.202.74hostnamesince unknown                   3AB  Gi1/0/1
192.168.202.75hostnamesince unknown                   3AB  Gi1/0/2
192.168.202.76hostnamesince unknown                   2BB  Gi4/0/17
192.168.202.77hostnamesince unknown                   REC  Gi2/0/19
192.168.202.78hostnamesince unknown                   1AD  Gi2/0/13
192.168.202.79hostnamesince unknown                   1AD  Gi2/0/16
192.168.202.83hostnamesince unknown                   GMB  Gi1/0/8
192.168.202.250hostnamesince Sun Jan 27 01:05:18 GMT 2013
192.169.4.20hostnamesince unknown
193.108.82.1hostnamesince Mon Aug 12 13:40:37 BST 2013

This information is picked up from multiple log files. As you can see 
the date, starting in field 4, is just the normal no-options output from 
'date'. I'd like to be able to filter this file based on the date, 
newest dates first. I can get pretty close to what I want but I'm 
struggling to filter down to the 'minute' level. Here's what I mean.

Running the command:

    cat unsorted.tst | sort -k 9n -Mk5 -k6n -k7n  | tac


produces something very close to what I want, e.g:

192.168.1.40hostnamesince Wed Aug 14 04:37:46 BST 2013
192.168.1.5hostnamesince Mon Aug 12 17:23:09 BST 2013      2BB  Gi2/0/2
192.168.1.43hostnamesince Mon Aug 12 17:23:25 BST 2013      2BB  Gi1/0/5
192.168.1.42hostnamesince Mon Aug 12 17:23:24 BST 2013      GMB  Gi1/0/14
192.168.1.37hostnamesince Mon Aug 12 17:23:21 BST 2013      GMB  Gi1/0/21
192.168.1.136hostnamesince Mon Aug 12 17:24:09 BST 2013      3AB  Gi1/0/1
192.168.1.135hostnamesince Mon Aug 12 17:24:07 BST 2013      RIV  Gi1/0/10
192.168.1.130hostnamesince Mon Aug 12 17:24:00 BST 2013      GMB  Gi1/0/21
192.168.1.129hostnamesince Mon Aug 12 17:23:59 BST 2013      2BB  Gi1/0/5
192.168.1.126hostnamesince Mon Aug 12 17:23:57 BST 2013      RIV  Gi1/0/13
192.168.1.125hostnamesince Mon Aug 12 17:23:55 BST 2013      2BB  Gi2/0/12
192.168.1.124hostnamesince Mon Aug 12 17:23:54 BST 2013      GMB  Gi1/0/14
192.168.1.11hostnamesince Mon Aug 12 17:23:19 BST 2013      2BB  Gi1/0/8
192.168.1.111hostnamesince Mon Aug 12 17:23:49 BST 2013      2BB  Gi1/0/12
192.168.1.106hostnamesince Mon Aug 12 17:23:48 BST 2013      2BB  Gi1/0/11
192.168.1.101hostnamesince Mon Aug 12 17:23:43 BST 2013      2BB  Gi1/0/13
192.168.202.26hostnamesince Mon Aug 12 16:51:03 BST 2013      REC  Gi1/0/3
192.168.1.4hostnamesince Mon Aug 12 16:55:49 BST 2013      2BB  Gi1/0/4
192.168.1.188hostnamesince Mon Aug 12 16:50:22 BST 2013      2BB  Gi1/0/5
192.168.1.137hostnamesince Mon Aug 12 16:49:56 BST 2013      3AB  Gi1/0/4
192.168.1.133hostnamesince Mon Aug 12 16:49:48 BST 2013      RIV  Gi1/0/1
192.168.0.12hostnamesince Mon Aug 12 16:45:40 BST 2013      GMB  Gi1/0/24
193.108.82.1hostnamesince Mon Aug 12 13:40:37 BST 2013
192.168.202.12hostnamesince Thu Aug  1 14:53:02 BST 2013      2BB  Gi4/0/11
192.168.0.211hostnamesince Thu Aug  1 09:54:58 BST 2013      REC  Gi1/0/24
192.168.0.13hostnamesince Tue Jul 30 12:47:07 BST 2013      GMB  Gi1/0/24
192.168.202.4hostnamesince Wed Jul 24 15:58:54 BST 2013      2BB  Gi3/0/7
192.168.0.34hostnamesince Tue Jul 23 12:21:01 BST 2013
192.168.0.33hostnamesince Tue Jul 23 12:21:00 BST 2013
192.168.0.32hostnamesince Tue Jul 23 12:20:58 BST 2013
192.168.202.6hostnamesince Mon Jul 22 16:22:13 BST 2013      2BB  Gi4/0/7
192.168.0.20hostnamesince Fri Jul 19 23:06:10 BST 2013      GMB  Gi1/0/24
192.168.2.104hostnamesince Fri Jul 19 16:43:18 BST 2013
192.168.1.167hostnamesince Fri Jul 19 16:28:13 BST 2013
192.168.0.23hostnamesince Thu Jul 18 11:39:54 BST 2013      3AB  Gi1/0/24

but if you look closely you can see that the minutes and hours are 
sometimes wrong in the Aug 12 rows.

I've experimented a lot with the -k7 option, dropping and adding the 
'n', and also after consulting the man page, trying something like this:

    sort -k 9n -k5M -k6n -k7.1,7.2n -k7.4,7.5n -k7.7,7.8n

which I thought would sort by HH,MM,SS ignoring the colons, in the time 
field. But it just produces a mess.

Alternatives include sorting out the problem at source - perhaps getting 
the original logfiles to include the date as YYYYMMDDHHMMSS but that's a 
non-trivial process. plus, I can't think I'm close but no cigar. I'm I 
missing something obvious with the sort command? Is there another way of 
doing this? It's got to drop into a shell script that gives me a daily 
report of host uptime/downtimes.

Dougie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/durham/attachments/20130814/433a3f7b/attachment-0001.html>


More information about the Durham mailing list