From: Lao Ming on 10 Aug 2010 01:45 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 10 Aug 2010 04:09 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 10 Aug 2010 04:13 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 10 Aug 2010 11:13 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.
|
Pages: 1 Prev: origin of command -v Next: #!/bin/sh 2>&1 |tee -a /tmp/logfile |