1846564 Members
2408 Online
110256 Solutions
New Discussion

Re: sorting date

 
hpuxrox
Respected Contributor

sorting date

Does anyone know the sort syntax to sort the following output from the current month starting from the 1st down?

07/23 testserver /test/trees/oradb02 29%
07/24 testserver /test/trees/oradb02 29%
07/25 testserver /test/trees/oradb02 28%
07/26 testserver /test/trees/oradb02 29%
07/27 testserver /test/trees/oradb02 29%
07/28 testserver /test/trees/oradb02 29%
07/29 testserver /test/trees/oradb02 33%
07/30 testserver /test/trees/oradb02 29%
07/31 testserver /test/trees/oradb02 30%
08/1 testserver /test/trees/oradb02 30%
08/2 testserver /test/trees/oradb02 30%
08/3 testserver /test/trees/oradb02 32%
08/4 testserver /test/trees/oradb02 32%
08/5 testserver /test/trees/oradb02 36%
08/6 testserver /test/trees/oradb02 32%
08/7 testserver /test/trees/oradb02 36%
08/8 testserver /test/trees/oradb02 32%
08/9 testserver /test/trees/oradb02 37%
08/10 testserver /test/trees/oradb02 33%
08/11 testserver /test/trees/oradb02 33%
08/12 testserver /test/trees/oradb02 32%
08/13 testserver /test/trees/oradb02 31%
08/14 testserver /test/trees/oradb02 36%
08/15 testserver /test/trees/oradb02 36%
08/16 testserver /test/trees/oradb02 36%
8 REPLIES 8
Sandman!
Honored Contributor

Re: sorting date

Please provide an example of how the output should look like. That would help in coming up with a viable solution.

~cheers
Geoff Wild
Honored Contributor

Re: sorting date

You can't sort that way.

What you could do is grep the current month and then sort, then re-grep the previous and sort and append, etc...etc...

grep ^`date +%m` somefile |sort -n

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
A. Clay Stephenson
Acclaimed Contributor

Re: sorting date

I would do an awk to first take the first field mm/dd and split to determine if the month matches the current month. I would then print the month and day as separate fields followed by the rest of the line. Sort that output and finally use another awk to print fields 3-NF.

--------------------------------------------
#!/usr/bin/sh

typeset -i STAT=0
typeset MO=$(date '+%m') # current month

awk -v month=${MO} \
'{ split($1,a,"/"); if ((a[1] + 0) == (month + 0)) {printf("%02d %02d ",a[1],a[2]); print $0; }}' | \
sort | \
awk '{for (i = 3; i <= (NF - 1); ++i) {printf("%s ",$i); print $NF}'
STAT=${?}
exit ${STAT}
--------------------------------------------

The logic looks right and the syntax should be ok. The stuff abount (month + 0) is correct though it appears strange; it forces the evaluation into numerical context rather than string.
If it ain't broke, I can fix that.
Hein van den Heuvel
Honored Contributor

Re: sorting date

I suggest you do yourself a favor and first reformat the data, and then sort.

Use SED or any tool you like. In perl I'd use:

$perl -pe 's#/(\d\s)#/0${1}#' bad > good
or with in-place editing:
$ perl -i.old -pe 's#/(\d\s)#/0${1}#' mydata

Of course you can sort with perl also. For example:
$ $ perl -ne '$data{$.}=$_;($m,$d)=split /[\/ ]/;$date{$.}=100*$m+$d}{foreach (sort {$date{$a}<=>$date{$b}} keys %date) {print $data{$_}}' y.txt

or

$ $ perl -e 'sub d {($m,$d)=split /[\/ ]/; return $m+100+$d} print sort {d($a)<=>d{$b}} <>' y.txt

The first solution load two arrays indexed by a line number, one with each data line and one with the data recalculated to be month times 100 + day. The sort the date array by value and print.

The next solution defines a function 'd' which gets that 100*M + D as return value for each record. Now print all input through a sort function comparing the 'd' function value for each input line.

Cheers,
Hein.


Sandman!
Honored Contributor

Re: sorting date

Imho grep(1) and sort(1) ought to do it if I understand your requirement i.e. to extract and sort only those records that belong to the current month. If so you could try the command below:

# grep $(date +%m) infile | sort -k1,1.2n -k1.4,1n
A. Clay Stephenson
Acclaimed Contributor

Re: sorting date

In case it's not obvious, the script I supplied would read stdin and write on stdout.

my.sh < infile > outfile
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: sorting date

Hi:

Similar to Sandman's reply, you could leverage the "/" as a field delimiter and do:

# sort -t/ -k1n -k2n file.in > file.out

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: sorting date

In case it's not obvious, I failed to correctly read the full request and jumped to answer a question not asked: sort all the data by date.

I concur with Sandman! that it would be good to see suggested sample output. For example it's not clear to me that if say it is the second of the month, should the result be just 2 lines?

Hein.