niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Perl
Need Help with Program in Perl on a Netware Server
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
fhadzocos@gmail.com
*nix forums beginner


Joined: 21 Jul 2006
Posts: 2

PostPosted: Fri Jul 21, 2006 1:57 pm    Post subject: Need Help with Program in Perl on a Netware Server Reply with quote

I have a huge folder with 44 thousand files I need to keep only the
last file for each type in that folder

ex

workstation1.001
workstation1.002
workstation2.49
workstation2.56
workstation20.560
workstation20.561
workstation20.562

In that example I need to keep workstation1.002 workstation2.56
workstation20.562 and this would continue until the end of the files.
There is only one folder to look in.
I have no idea how to go about doing this in perl. I'm very new to perl
and do not wish to make an inefficient program.

Thanks
Mobius1982
Back to top
fhadzocos@gmail.com
*nix forums beginner


Joined: 21 Jul 2006
Posts: 2

PostPosted: Fri Jul 21, 2006 2:07 pm    Post subject: Re: Need Help with Program in Perl on a Netware Server Reply with quote

Here are some of the real file names.

00A0D1B9233C_1153332333000_1.STR
00AD1B44961_1102616538000_556.STR

Thanks
Mobius1982
Back to top
Paul Lalli
*nix forums Guru


Joined: 10 Jun 2005
Posts: 1089

PostPosted: Fri Jul 21, 2006 2:24 pm    Post subject: Re: Need Help with Program in Perl on a Netware Server Reply with quote

fhadzocos@gmail.com wrote:
Quote:
I have a huge folder with 44 thousand files I need to keep only the
last file for each type in that folder

ex

workstation1.001
workstation1.002
workstation2.49
workstation2.56
workstation20.560
workstation20.561
workstation20.562

In that example I need to keep workstation1.002 workstation2.56
workstation20.562 and this would continue until the end of the files.
There is only one folder to look in.
I have no idea how to go about doing this in perl. I'm very new to perl
and do not wish to make an inefficient program.

First, I strongly recommend you create a program that *works* first,
and make it efficient second.

My recommendation is that you loop through the directory, grabbing each
file name. For each file, split it to get the basename and suffix
(see: perldoc File::Basename). Check if the basename exists in your
hash. If not, add it, with a value of the suffix. If it does exist,
compare the current suffix with the one that is already stored. If the
current one is less, remove the current file (perldoc -f unlink). If
the current one is greater, remove the file represented by the basename
and stored suffix, and replace the value with the current suffix.

That English description may be a bit confusing, (and we're always
telling people to Speak Perl rather than English), so here's a short
script to get you started.

#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;

opendir my $dh, $ARGV[0] or die "Cannot open directory $ARGV[0]: $!\n";

my %suffix_of;
while (my $file = readdir($dh)) {
next if $file =~ /^\.\.?$/ or ! -f "$ARGV[0]/$file";
my ($base, undef, $suffix) = fileparse($file, qr/\..*/);
$suffix =~ s/^\.//;
if (! exists $suffix_of{$base} ){
$suffix_of{$base} = $suffix;
}
elsif ($suffix_of{$base} > $suffix) {
unlink "$ARGV[0]/$file" or
warn "Could not remove $ARGV[0]/$file: $!\n";
}
else {
unlink "$ARGV[0]/$base.$suffix_of{$base}" or
warn "Could not remove $ARGV[0]/$base.$suffix_of{$base}:
$!\n";
$suffix_of{$base} = $suffix;
}
}


Paul Lalli
Back to top
Paul Lalli
*nix forums Guru


Joined: 10 Jun 2005
Posts: 1089

PostPosted: Fri Jul 21, 2006 2:26 pm    Post subject: Re: Need Help with Program in Perl on a Netware Server Reply with quote

fhadzocos@gmail.com wrote:
Quote:
Here are some of the real file names.

*WHY* didn't you include real data in your original post?

Quote:
00A0D1B9233C_1153332333000_1.STR
00AD1B44961_1102616538000_556.STR

In your original, you made it appear that by "last file" you meant
"file with the greatest suffix, numerically). How, exactly are you
determining "last file" now? And how are you grouping these files?

My program, which I spent a good 10 minutes writing, is not going to
work with your data. You have wasted my time by posting incorrect
requirements and data. That was very rude of you.

Paul Lalli
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Sat Nov 22, 2008 9:13 pm | All times are GMT
navigation Forum index » Programming » Perl
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Transfer qmail email account to postfix server tallman Postfix 0 Thu Jun 05, 2008 12:43 pm
No new posts NFS server on Solaris 10 gurgle Solaris 0 Tue Sep 04, 2007 7:05 pm
No new posts Help required for configuring the VPN Server in Linux SHERDIL security 0 Sun Nov 19, 2006 2:22 pm
No new posts can u solve this program :P bawen C++ 0 Mon Jul 24, 2006 6:26 pm
No new posts Problem while accessing 64 bit Server thru 32 bit client Ganesh Server 0 Fri Jul 21, 2006 1:40 pm

Magic The Gathering Cards | Myspace Proxy | Internet Advertising | Mortgages | Buy Anything On eBay
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1842s ][ Queries: 16 (0.0854s) ][ GZIP on - Debug on ]