From: Jack on
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.
From: Michael Paoli on
On May 24, 4:21 pm, Jack <junw2000(a)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=GMT0 date +'%Y-%M-%dT%H:%M:%S'
2010-05-25T03:05:54
$ LC_ALL=C sort | head -1

From: Jon LaBadie on
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).

From: Jack on
On May 24, 8:16 pm, Jon LaBadie <jlaba...(a)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?

Thanks.
From: Janis Papanagnou on
Jack wrote:
> On May 24, 8:16 pm, Jon LaBadie <jlaba...(a)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.