Perl find::file can I sort the out put


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl find::file can I sort the out put
# 1  
Old 12-10-2006
Perl file::find can I sort the out put

Perl file::find can I sort the out put
I am using file::find in my script but how I wish to process each file found in date order.

Can I sort this module?

eg

part of current script is....

use File::Find;
# Recursively find all files and directories in $mqueue_directory
find(\&wanted, $mqueue_directory);

my $nj=0;
sub wanted {
$nj++;
# print "In the wanted routine $nj $_\n";
# Is this a qf* file?
if ( /^qf\w/ ) {
etc
etc...
}

Last edited by Andrek; 12-10-2006 at 11:00 PM..
# 2  
Old 12-11-2006
Just make sure you capture the size of each file you want (together with the filename) in the wanted function and dump them to an array. Then you can post-process the list using sort().

Here is an example:
Code:
#!/usr/bin/perl -w

use File::Find;

my @files = ();
find(sub {
	if ($_ =~ /\.mp3$/) {
		push(@files, [
			$File::Find::dir . "/$_", 
			(stat($_))[7]
		]);
	}
}, "/home/bernardchan/scratch");
map {
	print "$$_[1]\t\t$$_[0]\n"
} sort {
	$$a[1] <=> $$b[1]
} @files;

Sample output:
Code:
3583250         /home/bernardchan/scratch/music/Heart - Alone.mp3
3790439         /home/bernardchan/scratch/music/S Club 7 - Dancing Queen.mp3
4003132         /home/bernardchan/scratch/music/bwv541.mp3
4272925         /home/bernardchan/scratch/music/toccata+fugue-bwv565.mp3
4276686         /home/bernardchan/scratch/music/toccata-bwv540.mp3
4434960         /home/bernardchan/scratch/music/Barry Manilow - The Old Songs.mp3
5840063         /home/bernardchan/scratch/music/j.s.bach-fantasia+fuga-bwv542.mp3

# 3  
Old 12-12-2006
Thanks, - I give it a go....
Andrek
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl program for find one entry and put in different file

Hi I have a file name1 xxxxx name1 xxxxx name1 yyyyy name 1 zzzzz name1 Uniprot Id 1234 name2 sssss name2 eeeee name2 bengamine name2 Uniprot Id 3456 ......................and so on I have to capture Uniprot IDs only in a separate file so that output contain only ... (20 Replies)
Discussion started by: manigrover
20 Replies

2. Solaris

How to sort df -h out put :

Hello every one , I am just trying to sort df -h out in a particular order to differentiate SAN disks and local disks .. does any body have any script or any useful command ?? thanks in advance .. (8 Replies)
Discussion started by: new2uniks
8 Replies

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

4. Shell Programming and Scripting

Perl SFTP, to get, sort and process every file.

Hi All, I'm niks, and i'm a newbie here and newbie also in perl sorry, i'm just wondering how can i get the file from the other hostname using sftp? then after i get it i'm going to sort the file and process it one by one. sorry because i'm a newbie. Thanks, -niks (4 Replies)
Discussion started by: nikki1200
4 Replies

5. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

6. Shell Programming and Scripting

Perl script to sort an Excel file

Hello! I need to sort a file that is partly in English partly in Bulgarian. The original file is an Excel file but I converted it to a tab-delimited text file. The encoding of the tab delimited file is UTF-8. To sort the text, the script should test every line of the text file to see if... (9 Replies)
Discussion started by: degoor
9 Replies

7. Shell Programming and Scripting

awk -- sort out the out put file

Hi, using awk /sed, I need to sort out the input file. here is the example input file=== Name= shashi | country= india | region = asia | dept= ww Name= jon | region = asia | dept= xx Name=sam | dept= ww Name= anthony | country=england | dept= xx Name= sumo | country= china output... (3 Replies)
Discussion started by: hegdeshashi
3 Replies

8. Shell Programming and Scripting

Perl Sort on Text File

Hi, I have a file of names and I want perl to do a sort on this file. How can I sort this list of names using perl? I'm thinking of a command like: @sorted = sort { lc($a) cmp lc($b) } @not_sorted # alphabetical sort The only thing I'm sort of unsure of is, how would I get the name in my... (6 Replies)
Discussion started by: eltinator
6 Replies

9. Shell Programming and Scripting

sort a file by date using perl

Hello, do any body help me to sort a file by date using perl? thanks in advance Esham (4 Replies)
Discussion started by: esham
4 Replies

10. Shell Programming and Scripting

Sort file in perl

Hi, I have an entry file for a perl script from which I need to remove duplicate entry. For example: one:two:three one:four:five two:one:three must become : one:two:three two:one:three The duplicate entry is only the first field. I try many options of sort system command but don't... (4 Replies)
Discussion started by: annececile
4 Replies
Login or Register to Ask a Question