find the last file containing a particular string

find the last file containing a particular string

Post by Lao Min » Wed, 11 Aug 2010 14:45:14


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.
 
 
 

find the last file containing a particular string

Post by Michael To » Wed, 11 Aug 2010 17:09:21


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'

 
 
 

find the last file containing a particular string

Post by pk » Wed, 11 Aug 2010 17:13:24


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.