views:

342

answers:

2

HI, I have a list of files from the last 7 days. From this list, if there are multiple files on a certain day, i need to get the latest for that day using korn shell script. Please help!

A: 

Something along the lines of:

newest=""
for f in $filelist ; do
  if [ "$f" -nt "$newest" ] ; then
    newest="$f"
  fi
done
Darryl
Thank you so much! This worked for me. But i am still looking for a way to compare two files from a list, check and see if they are created on the same day. any ideas?
A: 

You can use the following script:

ls -lt | egrep '^([^ ]+ +){5}Feb +3 +2009' | head -n 1

But if you are on solaris, /bin/egrep and /usr/bin/egrep don't support { } chars. I guess that behaviour is not according to standards. Anyway, in Solaris, you can use:

ls -lt | /usr/xpg4/bin/egrep '^([^ ]+ +){5}Feb +3 +2009' | head -n 1 | head -n 1

or

ls -lt | tr -s ' ' | egrep '^[^ ]+ [^ ]+ [^ ]+ [^ ]+ [^ ]+ Feb 3 2009' | head -n 1

You could also use ls and sed. But in Solaris, I don't think sed supports extended regexp.

Of course, replace Feb +3 +2009 with the date you want. Don't forget the '+' after the space.

Raze