Extracting files having maximum timestamp

Hi ,
I'm using Ksh 88
I've the following files in a directory with YearMonthDate (Ex:20130601)

YDT:FILE1:20130601
YDT:FILE1:20130615
YDT:FILE2:20130601
YDT:FILE2:20130615
YDT:FILE3:20130601
YDT:FILE3:20130615

And i need the files having maximum timestamp , Means i need to display

YDT:FILE1:20130615
YDT:FILE2:20130615
YDT:FILE3:20130615

Please suggest me the way to achieve this .

Thank You

Hello smile689,

Could you please let me know if you want to show only current date files?
as by seeing your Output, it seems the same. Please confirm so that I can try to help you.

Thanks,
R. Singh

Thank You for the reply Ravinder,

I want to show the file having latest timestamp for each file (Not the current date fiels )
Means for file1 it is having 2 timestamps with ( 20130601 ,20130615 )
and I need the latest timestamp file as output .

Please let me know if i'm not clear.

Thanks

Using associative arrays in ksh93 or modern bash:

typeset -A arr

for file in YDT:FILE*
do
        pfx="${file%:*}"
        sfx="${file##*:}"

        [ -z ${arr[$pfx]} ] && arr[$pfx]=$sfx
        [ ${arr[$pfx]} -lt $sfx ] && arr[$pfx]=$sfx
done

for k in ${!arr[@]}
do
        echo "$k:${arr[$k]}"
done

using awk:

ls YDT:FILE* | awk -F: '{a[$0]++};$3>d{d=$3}END{for(i in a){split(i,x,":");if(x[3]==d)print i}}'
1 Like

Thank You for your time Yoda,

I'm using Ksh 88 and getting the following err

typeset: bad option(s)

Using awk:

ls YDT:FILE* | awk -F: '
        {
                idx = $1 OFS $2
                if ( A[idx] -lt $3 )
                        A[idx] = $3
                else if ( ! ( A[idx] ) )
                        A[idx] = $3
        }
        END {
                for ( k in A )
                        print k OFS A[k]
        }
' OFS=:
1 Like

Hello smile689,

could you please try this, may be it will be helpful for you.

 
$ cat timings_file_query.ksh
 

echo "Please enter the number of files present types for files like file1, file2....till which file"
echo "eg--> lets say we have file1 to file30 so please enter 30 in the case"
 
 
read number_of_files

a=1

while [ ${a} -le ${number_of_files} ]
do

name=`echo "YDT:FILE"$a`
value_for_files_present_in_path=`ls -ltr | grep -v "grep" | grep $name | awk '{print$9}' | tail -1`
echo $value_for_files_present_in_path

let "a = a + 1"
done

 

Output is as follows:

 
$ ksh timings_file_query.ksh

Please enter the number of files present types for files like file1, file2....till which file
eg--> lets say we have file1 to file30 so please enter 30 in the case
2
YDT:FILE1:20130615
YDT:FILE2:20130622
$

 

Kindly let me know if you have any queries.

Thanks,
R. Singh

1 Like

Thank You So Much YODA, Subbesh
It worked in my script.