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
# 1  
Old 05-10-2012
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.
Code:
Ex.
File name       timestamp
P01_file1.txt  1PM
P02_file1.txt  1AM
P01_file2.txt  2PM
P03_file1.txt  2AM

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

Last edited by methyl; 05-10-2012 at 11:20 AM.. Reason: please ise code tags
# 2  
Old 05-10-2012
perl

Hi,

Try this out,


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

You can sort your file using sort command.
Code:
sort -k1,2 file

Cheers,
RangaSmilie
# 3  
Old 05-10-2012
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.
# 4  
Old 05-10-2012
Hi Ranga,
Thanks for the reply.
I am using the script to sort the files in a folder in order to timestamp:
Code:
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

Last edited by methyl; 05-10-2012 at 01:17 PM.. Reason: please use code tags
# 5  
Old 05-10-2012
sort

Quote:
Originally Posted by unankix
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)
@sorted_files=sort(@sorted_files);

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.
Dont take this req as a big deal. In perl, sort function sort the array as you required.

Cheers,
RangaSmilie
# 6  
Old 05-10-2012
Post an exact sample of the input and the expected output...
# 7  
Old 05-10-2012
Hi Shamrock,

I am using the code given to sort the files depending upon the timestamp:
Code:
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:
Code:
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:
Code:
P01_User2_124.txt (came first)
P01_User1_123.txt (came late)
P02_User1_231.txt
P03_User3_125.txt


Last edited by methyl; 05-10-2012 at 07:04 PM.. Reason: please use code tags
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