Sorting prob

Hi

I have a directory having following three set of files as follows:

test.20060804
test.20060801
test.20060802

I want to list the latest file which is less than 20060803. in this case, it will be test.20060802.

How can i achieve the same? Any pointers to above will be appreciated.

Thanks.

something to start with:

ls test.* | nawk -F'.' '{print $NF, $0}' | sort -rn | cut -d ' ' -f2-

Thanks for your reply. I understand you are trying to put the date part in first column in list output. But then how do you select the correct file? My requirement is to somehow pass '20060803' and get the file which is less than that date. If the result is more than one file, then pick the latest one. So, in this case, it will be test.20060802

Thanks for your help.

Perhaps something like this:

ls test.* | 
nawk -F. -v target_dt=20060803 '
    {
        if ($2 < target_dt && max_dt < $2) {
            max_dt=$2
            target_file=$0
    }
}
END {
    print target_file
}'

Results:

test.20060802

how 'bout.....

ls test.* | nawk -F'.' -v pivot='20060803' '$NF < pivot && $NF > latestDate {latest=$0;latestDate=$NF} END {print latest}'

tmarikle,
what can I say...... great minds........

You guys are awesome !!!

:smiley: too funny :smiley:
How often has that happened?