From: Simon@Vec on
Running ksh on Tru64 5.1.

In a directory I have a number of files:

abc_20051001.zip
abc_20051009.zip
abc_20050911.zip

Each file has the date of its creation as part of the name (yyyymmdd)
I am trying to write a script to get only the latest named file (may
not be the most recently modified), set as a variablem, then unzip it.

So in the above list abc_20051009 (9th Oct)would be the file to use.

Any ideas please?

From: Ed Morton on


Simon(a)Vec wrote:

> Running ksh on Tru64 5.1.
>
> In a directory I have a number of files:
>
> abc_20051001.zip
> abc_20051009.zip
> abc_20050911.zip
>
> Each file has the date of its creation as part of the name (yyyymmdd)
> I am trying to write a script to get only the latest named file (may
> not be the most recently modified), set as a variablem, then unzip it.
>
> So in the above list abc_20051009 (9th Oct)would be the file to use.
>
> Any ideas please?
>

sort -n <file> | tail -1

or

awk '$0>m{m=$0}END{print m}' file

Regards,

Ed.
From: Chris F.A. Johnson on
On 2005-10-11, Simon(a)Vec wrote:
> Running ksh on Tru64 5.1.
>
> In a directory I have a number of files:
>
> abc_20051001.zip
> abc_20051009.zip
> abc_20050911.zip
>
> Each file has the date of its creation as part of the name (yyyymmdd)
> I am trying to write a script to get only the latest named file (may
> not be the most recently modified), set as a variablem, then unzip it.
>
> So in the above list abc_20051009 (9th Oct)would be the file to use.

set -- abc_*.zip
eval "newest=\${$#}"
unzip "$newest"

--
Chris F.A. Johnson <http://cfaj.freeshell.org>
==================================================================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
From: Stein Arne Storslett on
<simon.forrest(a)vectornetworks.co.nz> wrote in <1129072941.110130.287980(a)g47g2000cwa.googlegroups.com>:
> Running ksh on Tru64 5.1.
>
> In a directory I have a number of files:
>
> abc_20051001.zip
> abc_20051009.zip
> abc_20050911.zip
>
> Each file has the date of its creation as part of the name (yyyymmdd)
> I am trying to write a script to get only the latest named file (may
> not be the most recently modified), set as a variablem, then unzip it.
>
> So in the above list abc_20051009 (9th Oct)would be the file to use.
>
> Any ideas please?


FMASK="abc_????????.zip"

FILE=$(ls -1 ${FMASK:?} | tail -1)

....

ls will sort the entries if you don't run ls with any particular sorting
swicthes.

--
Stein Arne
From: Chris F.A. Johnson on
On 2005-10-12, Stein Arne Storslett wrote:
><simon.forrest(a)vectornetworks.co.nz> wrote in <1129072941.110130.287980(a)g47g2000cwa.googlegroups.com>:
>> Running ksh on Tru64 5.1.
>>
>> In a directory I have a number of files:
>>
>> abc_20051001.zip
>> abc_20051009.zip
>> abc_20050911.zip
>>
>> Each file has the date of its creation as part of the name (yyyymmdd)
>> I am trying to write a script to get only the latest named file (may
>> not be the most recently modified), set as a variablem, then unzip it.
>>
>> So in the above list abc_20051009 (9th Oct)would be the file to use.
>>
>> Any ideas please?
>
>
> FMASK="abc_????????.zip"
>
> FILE=$(ls -1 ${FMASK:?} | tail -1)
>
> ...
>
> ls will sort the entries if you don't run ls with any particular sorting
> swicthes.

The shell will sort the entries without needing an external command.

set -- abc_*.zip
eval "newest=\${$#}"
unzip "$newest"

With a Bourne shell, it may be necessary to shift the args until
there are no more than 9, but it's not needed with a POSIX shell:

set -- abc_*.zip
while [ $# -gt 9 ]
do
shift 9
done
eval "newest=\${$#}"
unzip "$newest"

--
Chris F.A. Johnson <http://cfaj.freeshell.org>
==================================================================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>