1752273 Members
4735 Online
108786 Solutions
New Discussion

perl help/excel/delete

 
SOLVED
Go to solution
Shaf_1
Advisor

Re: perl help/excel/delete

The files are stored in a folder on my hard drive. I just need to select the 1000 files with no duplicates. No other criteria.
Michael Schulte zur Sur
Honored Contributor

Re: perl help/excel/delete

Hi,

you look for something like:
#!/bin/ksh
cd /tmp
for FILE in `ls | head -1000`
do
mv ${FILE} /pathto
done
?

greetings,

Michael
Shaf_1
Advisor

Re: perl help/excel/delete

Hello,

The program is not selecting 1000 random files. It is selecting the files in order. I am not sure why that is happening. I have over 3000 files and I need to select 1000 random files out of the 3000. Any ideas?

Thanks
Ermin Borovac
Honored Contributor

Re: perl help/excel/delete

I've attached a quick perl script.

$ random.pl

should return files in random order from directory .
Shaf_1
Advisor

Re: perl help/excel/delete

I tried using the script but I have a couple of questions. Here is your script with the information you told me to enter. I have a test folder set up with a total of 50 files for testing. I want to extract 20 random files from the 50 and mv them to another folder.

Did I enter the information correctly?

Can the files be moved from one folder to another?

Thanks



#!/usr/bin/perl

if (scalar(@ARGV) != 2) {
die "usage: $0 ~/documents/erule/test/test2 20\n";
}

@files = <$ARGV[0]/*>;
fisher_yates_shuffle(\@files);
$, = "\n";
print splice(@files, 0, $ARGV[1]);
print "\n";

sub fisher_yates_shuffle {
my $deck = shift; # $deck is a reference to an array
my $i = @$deck;
while ($i--) {
my $j = int rand ($i+1);
@$deck[$i,$j] = @$deck[$j,$i];
}
}
Ermin Borovac
Honored Contributor

Re: perl help/excel/delete

The script will return the names of 20 random files. If you want to move them to a different directory do the following

$ ./random.pl ~/documents/erule/test/test2 20 | while read file
do
echo mv $i
done

If printout is ok remove 'echo' from the last second line (before mv).
Ermin Borovac
Honored Contributor

Re: perl help/excel/delete

Oops, 'mv $i' should be 'mv $file'

$ ./random.pl ~/documents/erule/test/test2 20 | while read file
do
echo mv $file
done
Shaf_1
Advisor

Re: perl help/excel/delete

The first problem I am having is that the first script does not return any file names. I only get a line saying 'usage: randomfl.pl ~/documents/erule/test 20'. How will this code return my file names?

#!/usr/bin/perl

if (scalar(@ARGV) != 2) {
die "usage: $0 ~/documents/erule/test 20\n";
}

@files = <$ARGV[0]/*>;
fisher_yates_shuffle(\@files);
$, = "\n";
print splice(@files, 0, $ARGV[1]);
print "\n";

sub fisher_yates_shuffle {
my $deck = shift; # $deck is a reference to an array
my $i = @$deck;
while ($i--) {
my $j = int rand ($i+1);
@$deck[$i,$j] = @$deck[$j,$i];
}
}

My second problem is with the second script you told me to use. It is giving me errors. I am sure I am doing something wrong. Here is the script I used for that.

#!/usr/bin/perl
$ ./randomfl.pl ~/documents/erule/test 20 | while read file
do
echo mv $file ~/documents/erule/test/test2
done

What mistake am I making?
Michael Schulte zur Sur
Honored Contributor

Re: perl help/excel/delete

Hi,

the second script is actually a shell script and the $ was the prompt from the shell. Replace /usr/bin/perl with /usr/bin/sh
That should make it work.

greetings,

Michael
Ermin Borovac
Honored Contributor

Re: perl help/excel/delete

You don't need to make any modifications to the script. On the command line type

./randomfl.pl ~/documents/erule/test 20

That should return list of 20 randomly selected files.

I assume that you are running this on Linux.