How to get the oldest timestamps?

  • Follow


I have a file containing timestamps from the date command, for
example:
Thu May 13 13:51:36 PDT 2010
Wed May 12 11:51:36 PDT 2010
Mon Fed 10 10:08:20 PDT 2009

How to get the oldest timestamp? If I use a different "date" command,
will it be easier to get the oldest timestamp?

Thanks.
0
Reply junw2000 (221) 5/24/2010 11:21:12 PM

On May 24, 4:21=A0pm, Jack <junw2000@gmail.com> wrote:
> I have a file containing timestamps from the date command, for
> example:
> Thu May 13 13:51:36 PDT 2010
> Wed May 12 11:51:36 PDT 2010
> Mon Fed 10 10:08:20 PDT 2009
>
> How to get the oldest timestamp? If I use a different "date" command,
> will it be easier to get the oldest timestamp?

$ TZ=3DGMT0 date +'%Y-%M-%dT%H:%M:%S'
2010-05-25T03:05:54
$ LC_ALL=3DC sort | head -1

0
Reply Michael 5/25/2010 3:08:03 AM


Jack wrote:
> I have a file containing timestamps from the date command, for
> example:
> Thu May 13 13:51:36 PDT 2010
> Wed May 12 11:51:36 PDT 2010
> Mon Fed 10 10:08:20 PDT 2009
> 
> How to get the oldest timestamp? If I use a different "date" command,
> will it be easier to get the oldest timestamp?
> 
> Thanks.

You could use a complicated sort command. (untested)

sort -r -k6n,7 -k2M,3 -k3n,4 -k4.1n,4.3 -k4.4n,4.6 -k4.7n4.9 | head -1

Or you could save simpler time stamps with

   date +%Y%m%d%H%M%S

Or with gnu date you could save 'epochtime' with

   date +%s

With either of the last you could simply sort -n and pipe to tail -1
(or sort -rn and pipe to head -1).

0
Reply Jon 5/25/2010 3:16:18 AM

On May 24, 8:16=A0pm, Jon LaBadie <jlaba...@aXcXm.org> wrote:
> Jack wrote:
> > I have a file containing timestamps from the date command, for
> > example:
> > Thu May 13 13:51:36 PDT 2010
> > Wed May 12 11:51:36 PDT 2010
> > Mon Fed 10 10:08:20 PDT 2009
>
> > How to get the oldest timestamp? If I use a different "date" command,
> > will it be easier to get the oldest timestamp?
>
> > Thanks.
>
> You could use a complicated sort command. (untested)
>
> sort -r -k6n,7 -k2M,3 -k3n,4 -k4.1n,4.3 -k4.4n,4.6 -k4.7n4.9 | head -1
>
> Or you could save simpler time stamps with
>
> =A0 =A0date +%Y%m%d%H%M%S
>
> Or with gnu date you could save 'epochtime' with
>
> =A0 =A0date +%s
>
> With either of the last you could simply sort -n and pipe to tail -1
> (or sort -rn and pipe to head -1).

If I have two timesamps obtained from " date +%Y%m%d%H%M%S ", how to
compare them to get the older timestamp?

Thanks.
0
Reply Jack 5/25/2010 5:40:09 AM

Jack wrote:
> On May 24, 8:16 pm, Jon LaBadie <jlaba...@aXcXm.org> wrote:
>> Jack wrote:
>>> I have a file containing timestamps from the date command, for
>>> example:
>>> Thu May 13 13:51:36 PDT 2010
>>> Wed May 12 11:51:36 PDT 2010
>>> Mon Fed 10 10:08:20 PDT 2009
>>> How to get the oldest timestamp? If I use a different "date" command,
>>> will it be easier to get the oldest timestamp?
>>> Thanks.
>> You could use a complicated sort command. (untested)
>>
>> sort -r -k6n,7 -k2M,3 -k3n,4 -k4.1n,4.3 -k4.4n,4.6 -k4.7n4.9 | head -1
>>
>> Or you could save simpler time stamps with
>>
>>    date +%Y%m%d%H%M%S
>>
>> Or with gnu date you could save 'epochtime' with
>>
>>    date +%s
>>
>> With either of the last you could simply sort -n and pipe to tail -1
>> (or sort -rn and pipe to head -1).
> 
> If I have two timesamps obtained from " date +%Y%m%d%H%M%S ", how to
> compare them to get the older timestamp?

Michael Paoli seems to have already answered what you're asking for.
Use  tail -n 1  or  head -n 1  if your file has the timestamps sorted
(ascending/descending), otherwise, if the timestamps in your file are
unsorted, just use sort on your file before you apply head (or tail).

If you have the timestamps not in a file as you said in your OP, but
rather in variables then use the shell's test operator to compare the
variables, (in the above case with ISO dates) like  [ $t1 < $t2 ] ,
where variables t1 and t2 contain the timestamp values.

If neither fits, clarify in what way your timestamps are organized, or
explain your problems with the given proposals.

Janis

> 
> Thanks.
0
Reply Janis 5/25/2010 7:36:23 AM

On May 24, 10:40=A0pm, Jack <junw2000@gmail.com> wrote:
> On May 24, 8:16=A0pm, Jon LaBadie <jlaba...@aXcXm.org> wrote:
> > Jack wrote:
> > > I have a file containing timestamps from the date command, for
> > > example:
> > > Thu May 13 13:51:36 PDT 2010
> > > Wed May 12 11:51:36 PDT 2010
> > > Mon Fed 10 10:08:20 PDT 2009
> > > How to get the oldest timestamp? If I use a different "date" command,
> > > will it be easier to get the oldest timestamp?
> > You could use a complicated sort command. (untested)
> > sort -r -k6n,7 -k2M,3 -k3n,4 -k4.1n,4.3 -k4.4n,4.6 -k4.7n4.9 | head -1
> > Or you could save simpler time stamps with
> > =A0 =A0date +%Y%m%d%H%M%S
> If I have two timesamps obtained from " date +%Y%m%d%H%M%S ", how to
> compare them to get the older timestamp?

That:
$ date +%Y%m%d%H%M%S
won't always work.
Hints: timezones, daylight saving time / summer time

Hence, on
news:04e6a7df-4342-4596-bf0c-c8d1e8367ecd@y6g2000pra.googlegroups.com
I suggested:
> $ TZ=3DGMT0 date +'%Y-%M-%dT%H:%M:%S'
> 2010-05-25T03:05:54
> $ LC_ALL=3DC sort | head -1
As that date command as shown will work and sort (as shown)
consistently, as long as one's system times are correct, or at least
consistent.
0
Reply Michael 5/29/2010 1:13:34 PM

5 Replies
611 Views

(page loaded in 0.092 seconds)

Similiar Articles:













7/24/2012 7:10:59 AM


Reply: