Sorting of YYYY-MM-DD

Hi ,

I have a script which basically reports the following criteria when run with a directory as an argument

Filecount    size   Directory name owner  group owner    last modified time

Example entry

1       100189 bytes      97.8408 KB    0.0955477 MB    /var/log/mongo          mongod mongod   2013-10-22

Now I'm trying to sort the output of the script as per the last modified time stamp ( I'm testing ,thats the reason Im awking the 11 field )

[root@#### bin]# sh report.sh -d /var/log  | sort -n -k 11.4 -k 11.5 -k 11.9  -k 11 | awk '{print $11}'

Well my sort command is working on YYYY-MM-DD format OK, how ever it is not sorting DD properly

Following is the sample output and the ones in bold are wrong

2012-08-31
2012-08-31
2013-02-17
2013-02-28
2013-02-28
2013-02-28
2013-02-28
2013-02-21
2013-04-23
2013-05-21
2013-10-22
2014-03-18
2014-04-27
2014-07-25
2014-07-29
2014-07-27
2014-07-27
2014-07-29
2014-07-05
2014-07-24
2014-07-28
2014-07-25

How do I sort correctly

Thanks in advance

you're trying too hard. perhaps:

sh report.sh -d /var/log  | awk '{print $11} | sort -n'

well, anyway, i couldn't reproduce your output.. sort -k11n should be enough though.

You're still trying too hard, as long as you have YYYYMMDD or YYYY-MM-DD or YYYY/MM/DD you don't need -n and when you have non-numeric characters in the string (in this case the minus-signs separating month and day), you don't want the -n . And the last single quote is in the wrong spot. For this you just need:

sh report.sh -d /var/log  | awk '{print $11}' | sort

or from the original post:

sh report.sh -d /var/log  | sort -k 11,11 | awk '{print $11}'

But, rather than counting fields, in this case I'd be tempted to use:

sh report.sh -d /var/log  | awk '{print $NF}' | sort

I have used

awk '{print $NF}' | sort

works absolutely fine !

However when I use it with the collective output it does not.

Example

 18      9150123 bytes      8935.67 KB    8.72624 MB     /var/log/sa     root   root     2014-07-31
 6       53170 bytes      51.9238 KB    0.0507069 MB     /var/log/xen    root   root     2014-07-25
 0       0 bytes      0 KB    0 MB       /var/log/xen/console    root   root     2013-05-21
 33      13332227 bytes      13019.8 KB    12.7146 MB    /var/log/nmon   nobody nobody   2014-07-31
 0       0 bytes      0 KB    0 MB       /var/log/squid          squid  squid    2010-03-31
 0       0 bytes      0 KB    0 MB       /var/log/iptraf         root   root     2007-03-14
 1       1448 bytes      1.41406 KB    0.00138092 MB     /var/log/mail   root   root     2011-08-11
 3       0 bytes      0 KB    0 MB       /var/log/news   news   news     2012-08-31
 0       0 bytes      0 KB    0 MB       /var/log/news/OLD       news   news     2010-01-27
 1       100189 bytes      97.8408 KB    0.0955477 MB    /var/log/mongo          mongod mongod   2013-10-22

Any ideas
#####################################################################################################################################

Found what works for me

[root@#####bin]# sh get.sh -d /var/log  | sort -s -k 11.1,11.4 -k 11.6,11.7 -k 11.9,11.10 -k11.10 | awk '{print $11}'
2007-03-14
2007-11-11
2007-11-11
2009-01-21
2009-09-20
2010-01-27
2010-03-31
2011-08-11
2012-08-31
2012-08-31
2012-08-31
2013-02-17
2013-02-21
2013-02-28
2013-02-28
2013-02-28
2013-02-28
2013-04-23
2013-05-21
2013-10-22
2014-03-18
2014-04-27
2014-07-05
2014-07-24
2014-07-25
2014-07-25
2014-07-27
2014-07-27
2014-07-31
2014-07-31
2014-07-31

Many thanks

When I feed the input shown in your last message into the pipeline:

awk '{print $NF}' | sort

I get the output:

2007-03-14
2010-01-27
2010-03-31
2011-08-11
2012-08-31
2013-05-21
2013-10-22
2014-07-25
2014-07-31
2014-07-31

which looks like the correct output to me.

I have no idea how you got 31 lines of output from those 10 input lines???

I believe he is trying to sort all the data, and just extracting the list of dates to make it easier to check...

What separator does your data use? Tabs, or multiple spaces between elements?