I have been racking my brain for a while on this..... was hoping one of the guru's could point me in the right direction....
Basically I have the following data in a file :
----
2009-01-01 10665 Begin 02:25:23
2009-01-02 10665 Begin 20:54:11
2009-01-03 10665 Begin 05:31:17
2009-01-03 10666 Begin 06:01:38
2009-01-03 10667 Begin 06:33:18
2009-01-03 10668 Begin 07:00:42
2009-01-03 10669 Begin 07:28:14
2009-01-03 10670 Begin 07:56:09
2009-01-06 10671 Begin 00:25:31
2009-01-06 10671 Begin 02:17:50
2009-01-06 10671 Begin 03:55:32
----
I want to end up with :
------
2009-01-02 10665 Begin 20:54:11
2009-01-03 10666 Begin 06:01:38
2009-01-03 10667 Begin 06:33:18
2009-01-03 10668 Begin 07:00:42
2009-01-03 10669 Begin 07:28:14
2009-01-03 10670 Begin 07:56:09
2009-01-06 10671 Begin 03:55:32
-------
I have tried using sort (-u -kn) and in reverse. but either way I do it I always end up getting the first instance of sort on column 2.
e.g. I get
2009-01-06 10671 Begin 00:25:31
instead of :
2009-01-06 10671 Begin 03:55:32
I used : sort -k 1.12,1.16 -u n.out so far ~(nearest try)
Not sure I understand what you're aiming for here.
It looks like the for each possible value of the second field you are wanting to return only one line, but what criteria are you using to decide on your return. e.g.:
First three lines:
2009-01-01 10665 Begin 02:25:23
2009-01-02 10665 Begin 20:54:11
2009-01-03 10665 Begin 05:31:17
Last three lines:
2009-01-06 10671 Begin 00:25:31
2009-01-06 10671 Begin 02:17:50
2009-01-06 10671 Begin 03:55:32
lines highlighted in bold appear to be the ones you're after, but I do not see quite how you decided on these...
Is it simply the line with the greatest timestamp in the third field that you want returned for each value in the second field, regardless of the date in the first field?
In which case - can be leaned up somewhat, but:
# sort -n +3 infile | awk '{t[$2]=$0}END{for (i in t){print t}}' | sort -n +1
2009-01-02 10665 Begin 20:54:11
2009-01-03 10666 Begin 06:01:38
2009-01-03 10667 Begin 06:33:18
2009-01-03 10668 Begin 07:00:42
2009-01-03 10669 Begin 07:28:14
2009-01-03 10670 Begin 07:56:09
2009-01-06 10671 Begin 03:55:32
If you wont to take the last instance instead of first one in column 2, you can do it like that:
sort -r file | uniq -s11 -w5 | sort
But than the output looks like this:
2009-01-03 10665 Begin 05:31:17
2009-01-03 10666 Begin 06:01:38
2009-01-03 10667 Begin 06:33:18
2009-01-03 10668 Begin 07:00:42
2009-01-03 10669 Begin 07:28:14
2009-01-03 10670 Begin 07:56:09
2009-01-06 10671 Begin 03:55:32
It differs in the first line with what you specified.
Thanks you very much for all your responses to my madness.
I need to the sort on column 2 but only pull out the value with was the greatest timestamp. sorry should have made that clear... bad example data I put here. thanks Tytalus, as your code worked a treat.
Am a happy bunny now. thank to the rest of you guys too, will try some of the other ways also.