Sort the files on their name and then the timestamp using Perl


 
Thread Tools Search this Thread
Top Forums Programming Sort the files on their name and then the timestamp using Perl
# 8  
Old 05-10-2012
Please explain in detail where the timestamp comes from? In the latest example it does not appear to be component of the filename.
# 9  
Old 05-11-2012
Yes, The timestamp is not the part of the file name.it is the system timestamp when file landed to that folder.
# 10  
Old 05-11-2012
perl

Hi,

Try this one. I think this is what you are looking for.

Code:
#! /usr/bin/perl
 
use strict;
use warnings;
 
my $location = "/home/name/files";
my ($cur_priority, $ost, $sst, $file, $sfile) = "";
 
opendir(DIR,"$location");
my @files=sort(grep{/^[^.]/} readdir(DIR));
print "Before Sort: @files\n";
close(DIR);
 
for(my $i=0;$i<scalar(@files);$i++)
{
 $file = sprintf("%s/%s","$location",$files[$i]);
 $ost = (stat($file))[9];
 $cur_priority = (split(/\_/,$files[$i]))[0];
 for(my $j=0;$j<scalar(@files);$j++)
 {
    if ( $files[$j] =~ /^$cur_priority/ )
    {
       $sfile = sprintf("%s/%s","$location",$files[$j]);
       $sst = (stat($sfile))[9];
       if ( $ost < $sst )
       {
          my $temp = $files[$i];
          $files[$i] = $files[$j];
          $files[$j] = $temp;
       }
    }
 }
}
print "After Sort: @files\n";

Time stamp sorting part is bolded.

Cheers,
RangaSmilie
# 11  
Old 05-11-2012
Thanks for the replies.
My code:
my @files=sort -k1,15 (grep{/^[^.]/} readdir(DIR));
is not working.
In this I am trying to sort the files in a folder depending upon the first 15 characters of the file name.
Error:syntax error at line 17, near "15 ("

Thanks.
# 12  
Old 05-14-2012
Hi All,


Any help to sort the files in a folder depending upon first 15 characters of their name will be highlt appriciated. I need it urgently.
File name: PNN_NN_YYYYMMDD_YYYYJLN_XX.txt
All the files will follow the naming standard mentioned above. I need to sort them on the values till YYYYMMDD part. Next part is nt important for us. NN is Numeric value.

Thanks.
# 13  
Old 05-15-2012
Quote:
I need to sort them on the values till YYYYMMDD part. Next part is nt important for us. NN is Numeric value.
Isn't that the same order as ls -1 ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find files and sort by timestamp

Used below command to get list of files sorted by timestamp find -L . -type f -name '*dat*' | xargs ls -ltrg I want to get only the filenames so I tried adding basename but it doenst work , can some one advise on how to get only file name (1 Reply)
Discussion started by: lalitpct
1 Replies

2. Shell Programming and Scripting

Sort Unique Column with the most recent timestamp

Hello, I have a sample file with the below contents : Backup Oracle8_P112_PEGA_Archivedel Completed full 10/11/2015 03:50:06PM Backup Oracle8_G567_PEGA_Archivedel Completed full 10/11/2015 01:15:56PM Backup Oracle8_P112_PEGA_Archivedel Completed full ... (8 Replies)
Discussion started by: rahul2662
8 Replies

3. Shell Programming and Scripting

How to sort the timestamp in the filename in shell script?

originally the shellscript #ln_file_name=`echo $ld_interface_date"_"${8}".csv"` #ln_file_name=`echo 201202011527_HL_HLTM1_B04A.csv` ln_file_name="*"`echo ${7}".csv"` get_file_list_1=$log_path"tm1_file_list.gfl1" cd ${source_path} echo "Try to find any file exist in the... (10 Replies)
Discussion started by: feilhk
10 Replies

4. Shell Programming and Scripting

How to sort grep result based on timestamp?

Hi, Trying to sort grep result based on timestamp of the filename. I have the following result and want to sort them on timestampgrep -i 'ERROR' *log*2013* s_m_xxx_xxx_xxx_xxx_xxxx.log.20130906092431:TRANSF_1_1_1> DBG_21216 Finished transformations for Source Qualifier . Total errors ... (5 Replies)
Discussion started by: bobbygsk
5 Replies

5. 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

6. Homework & Coursework Questions

Shell script calling Perl function, sort and find data, write to new files

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I must write a shell script that calls two external Perl functions--one of which sorts the data in a file, and... (6 Replies)
Discussion started by: kowit010
6 Replies

7. 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

8. UNIX for Dummies Questions & Answers

Shell script which will sort all the files in a directory with the timestamp they were created

Team, Pls help writing a shell script which will sort all the files in a directory with the timestamp they were created. (like ls -lrt) (6 Replies)
Discussion started by: asappidi
6 Replies

9. Shell Programming and Scripting

sort for timestamp information-YYYY-MM-DD HH:MM:SS

Dear all, Please advice how do I sort a file based on timestamp information. I want to sort the second column in asc/desc order which has timestamp information in format YYYY-MM-DD HH:MM:SS Example File - Input.txt contains cat ss.txt 100|2009-03-30 11:38:43 141|2009-06-01 12:12:01... (1 Reply)
Discussion started by: sureshg_sampat
1 Replies

10. UNIX for Dummies Questions & Answers

sort file on timestamp and a alphabetic code in it

Hi, I have files like ER08EVENTDC080911232324.txt ER08EVENTCD080911232323.txt ER08EVENTCD080910222222.txt ER08EVENTCD080910130245.txt ER08EVENTAA080910130245.txt ER08EVENTAA080910232000.txt ER08EVENTAA080911015005.txt ER08EVENTAA080910130150.txt ER08EVENTAA080910232010.txt... (5 Replies)
Discussion started by: harshada
5 Replies
Login or Register to Ask a Question