From: Lao Ming on
This should be really easy but I am perplexed.

I have a directory with about 500-600 files with filenames in two
different name formats. The files are either named according to
yymmdd i.e. 100809 or yymmdd.ext i.e 100809.txt -- how can I find the
last occurrence (by the date found in the filename) of a particular
string (in this case, "CSLIP")?

Among several variations, I tried:


lastfile=$( grep "CSLIP" `ls -1` | tail -1 )

but the files with extensions are always listed first in this way
(even though ls -1 is, of course, sorted properly at the shell). I
don't get it.

Thanks.
From: Michael Tosch on
Lao Ming wrote:
> This should be really easy but I am perplexed.
>
> I have a directory with about 500-600 files with filenames in two
> different name formats. The files are either named according to
> yymmdd i.e. 100809 or yymmdd.ext i.e 100809.txt -- how can I find the
> last occurrence (by the date found in the filename) of a particular
> string (in this case, "CSLIP")?
>
> Among several variations, I tried:
>
>
> lastfile=$( grep "CSLIP" `ls -1` | tail -1 )
>
> but the files with extensions are always listed first in this way
> (even though ls -1 is, of course, sorted properly at the shell). I
> don't get it.
>
> Thanks.

If created in that order you can use "ls -t"

#!/bin/sh
ls -t |
while read -r file
do
grep -l CSLIP "$file" && break
done



--
echo imhcea\.lophc.tcs.hmo |
sed 's3\(....\)\(.\{5\}\)3\2\132;s2\(.\)\(\)\(.\)2\3\12g;1s;\.;::;2'
From: pk on
Lao Ming wrote:

> I have a directory with about 500-600 files with filenames in two
> different name formats. The files are either named according to
> yymmdd i.e. 100809 or yymmdd.ext i.e 100809.txt -- how can I find the
> last occurrence (by the date found in the filename) of a particular
> string (in this case, "CSLIP")?
>
> Among several variations, I tried:
>
>
> lastfile=$( grep "CSLIP" `ls -1` | tail -1 )
>
> but the files with extensions are always listed first in this way
> (even though ls -1 is, of course, sorted properly at the shell). I
> don't get it.

You can try something like

gawk '/CSLIP/{
date=substr(FILENAME,1,6)
if (date >= maxdate){
maxdate=date
maxfile=FILENAME
}
nextfile
}
END{
print maxfile
}' *


(untested)

Depending on what you want to do with files that have the same date, you may
want to tweak the >= in the comparison.
From: Ben Bacarisse on
Lao Ming <laomingliu(a)gmail.com> writes:

> This should be really easy but I am perplexed.
>
> I have a directory with about 500-600 files with filenames in two
> different name formats. The files are either named according to
> yymmdd i.e. 100809 or yymmdd.ext i.e 100809.txt -- how can I find the
> last occurrence (by the date found in the filename) of a particular
> string (in this case, "CSLIP")?
>
> Among several variations, I tried:
>
> lastfile=$( grep "CSLIP" `ls -1` | tail -1 )
>
> but the files with extensions are always listed first in this way
> (even though ls -1 is, of course, sorted properly at the shell). I
> don't get it.

No, I am not sure I understand why you are having a problem. grep
should look at the files in order and if ls -1 is in the right order so
should the output of grep. `ls -1` can have problems when files contain
spaces and other special characters.

I might go with

grep CSLIP * | sort | tail -1

Use grep -l if you only want the file name; grep -h if you only want the
last matching line.

--
Ben.