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

Hi All,

I am new to Perl. I have a scenario to code. In a folder I have number of files and they will start with P01 or P02 or P03 and so on..I have to sort them on name first and then on time stamp.

Ex.
File name       timestamp
P01_file1.txt  1PM
P02_file1.txt  1AM
P01_file2.txt  2PM
P03_file1.txt  2AM
So the result should be:
P01_file1.txt  1PM
P01_file2.txt  2PM
P02_file1.txt  1PM
P03_file1.txt  2AM

I need to get the file names in the sorted order (first by name and then by timestamp) only.
Thanks

Hi,

Try this out,

perl -ne '$a{$_}=1;END{foreach my $k (sort keys %a){print $k;}}' file

You can sort your file using sort command.

sort -k1,2 file

Cheers,
Ranga:)

In your example, what happened to P02_file1.txt 1AM ?
On the surface the output order would be the same as ls -1 , but that assumes that all the times are single digit numbers.

Hi Ranga,
Thanks for the reply.
I am using the script to sort the files in a folder in order to timestamp:

opendir(DIR,$directory)|| die "Error in opening dir $directory\n";
my @sorted_files =
map $_->[1],
reverse sort { $a->[0] <=> $b->[0] }
map -f "$directory/$_" ? [ ( stat _ )[9], $_ ] : (),
readdir(DIR);
closedir(DIR)

Can you please let me know if there is any way I can modifythis script to first sort the files on name and then on Timestamp.
If you will explain your answer,that will be great help.
Thanks.

---------- Post updated at 10:23 AM ---------- Previous update was at 10:18 AM ----------

Hi Methyl,

I want to sort the files first on name.and then sort the sorted files on timestamp. That means even the file P02_file1.txt is the earliest file as per the timestamp but due to name it will come after all P01 files.If there is any other file with P02(ex.P02_file2.txt 2AM) then P02_file1.txt will come before that.

Thanks

Dont take this req as a big deal. In perl, sort function sort the array as you required.

Cheers,
Ranga:)

Post an exact sample of the input and the expected output...

Hi Shamrock,

I am using the code given to sort the files depending upon the timestamp:

opendir(DIR,$directory)|| die "Error in opening dir $directory\n";
my @sorted_files =
map $_->[1],
reverse sort { $a->[0] <=> $b->[0] }
map -f "$directory/$_" ? [ ( stat _ )[9], $_ ] : (),
readdir(DIR);
closedir(DIR)

My scenario is:
in folder i have thse files:

P01_User1_123.txt (came late)
P01_User2_124.txt (came first)
P02_User1_231.txt
P03_User3_125.txt

Where P01,P02 and so on are the Priority in which files should be ordered. If the priority is the same then as per the timestamp(first come first placed).
That means:

P01_User2_124.txt (came first)
P01_User1_123.txt (came late)
P02_User1_231.txt
P03_User3_125.txt

Please explain in detail where the timestamp comes from? In the latest example it does not appear to be component of the filename.

Yes, The timestamp is not the part of the file name.it is the system timestamp when file landed to that folder.

Hi,

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

#! /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,
Ranga:)

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.

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.

Isn't that the same order as ls -1 ?