Extracting files having maximum timestamp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting files having maximum timestamp
# 1  
Old 07-09-2013
Extracting files having maximum timestamp

Hi ,
I'm using Ksh 88
I've the following files in a directory with YearMonthDate (Ex:20130601)
Code:
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
Code:
YDT:FILE1:20130615
YDT:FILE2:20130615
YDT:FILE3:20130615

Please suggest me the way to achieve this .

Thank You
Moderator's Comments:
Mod Comment use code tags next time!!

Last edited by smile689; 07-09-2013 at 11:49 AM.. Reason: Updating version
# 2  
Old 07-09-2013
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
# 3  
Old 07-09-2013
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
# 4  
Old 07-09-2013
Using associative arrays in ksh93 or modern bash:
Code:
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

# 5  
Old 07-09-2013
using awk:

Code:
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}}'

This User Gave Thanks to Subbeh For This Post:
# 6  
Old 07-09-2013
Thank You for your time Yoda,

I'm using Ksh 88 and getting the following err
Code:
typeset: bad option(s)

# 7  
Old 07-09-2013
Using awk:
Code:
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=:

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting values after the maximum value in a txt file

Hello, I'm new to scripting and I need to write a bash script. Here is example of file on which I'm working: 0.3092381 0.3262799 0.3425480 0.3578379 0.3719490 0.3846908 0.3958855 0.4053738 0.4130160 0.4186991 0.4223357 0.4238688 ... (14 Replies)
Discussion started by: jeo_fb
14 Replies

2. Debian

Problem with maximum files and directories

Hi, I'm using Kali linux, I think it's a debian linux ? I'm trying to create a folder in which there'll be 256 folders, and in each of this folders there will also be 256 folders. Then in each terminate folders I want to create 4096 files. It will look like /dir/aa/aa/aaa.txt,... (3 Replies)
Discussion started by: ganon551
3 Replies

3. Shell Programming and Scripting

Finding the maximum timestamp in a folder

I've the files in a directory in the following format having date +%Y%m%d%H YR_MNTH_2013061205 YR_MNTH_2013060107 and i need the latest file i.e; YR_MNTH_2013061205 to be moved to another folder #!/bin/ksh # Ksh 88 Version for test_time in YR* do --- done How can i achieve that !... (2 Replies)
Discussion started by: smile689
2 Replies

4. Shell Programming and Scripting

Identifying files with a timestamp greater than a given timestamp

I need to be able to identify files with file timestamps greater than a given timestamp. I am using the following solution, although it appears to compare files at the "seconds" granularity and I need it at the milliseconds. When I tested my solution, it missed files that had timestamps... (3 Replies)
Discussion started by: nkm0brm
3 Replies

5. Shell Programming and Scripting

sort the files based on timestamp and execute sorted files in order

Hi I have a requirement like below I need to sort the files based on the timestamp in the file name and run them in sorted order and then archive all the files which are one day old to temp directory My files looks like this PGABOLTXML1D_201108121235.xml... (1 Reply)
Discussion started by: saidutta123
1 Replies

6. Shell Programming and Scripting

Sub directories containing maximum files

Hi All, I have this command coded in C Shell to get the top ten sub directories in the order of number of files they contain. find $parent_dir -type d -exec filecount {} \; | sort -nr | head -10 But it does not seem to show any output. Can someone please help me out in correcting this... (5 Replies)
Discussion started by: adurga
5 Replies

7. UNIX for Dummies Questions & Answers

ls - maximum number of files

what is the maximum number ls can list down (6 Replies)
Discussion started by: karnan
6 Replies

8. HP-UX

to get the timestamp of files from the files and folders in Unix

Hi, I had a directory and many subdirectories and files with in it. Now i want to get the timestamp of files from the files and folders recursively. :( Please help me to generate a script fort he above mentioned requirement! Appreciate for ur qick response Thanks in advance! ... (2 Replies)
Discussion started by: kishan
2 Replies

9. UNIX for Dummies Questions & Answers

Unix shell script for finding top ten files of maximum size

I need to write a Unix shell script which will list top 10 files in a directory tree on basis of size. i.e. first file should be the biggest in the whole directory and all its sub directories. Please suggest any ideas (10 Replies)
Discussion started by: abhilashnair
10 Replies

10. UNIX for Dummies Questions & Answers

Maximum files in a Directory

Does Solaris impose limits on : - the maximum number of files a directory can have, - total file size in a directory If there is such limits, how can I can check for each? Thanks...:confused: (1 Reply)
Discussion started by: deaniyoer
1 Replies
Login or Register to Ask a Question