regular expressions and renaming files

D

David Lebel

Hi,

as a way to teach myself Perl, I started to convert my toolbox of
scripts from (mostly) Perl to Python. One script I had in my toolbox
was used to rename files using a regular expression. That script came
from Tom Christiansen (of Perl fame) and shows the great flexibility of
Perl doing regular expression operations.

Now, I don't know where to start doing the conversion.

Any insights?

#!/usr/bin/env perl

# Tom Christiansen <[email protected]>, in alt.sources, 90-12-08.
# Modified by François Pinard <[email protected]>, 91-04-19.
#
# rename script examples from lwall:
# rename 's/\.orig$//' *.orig
# rename 'y/A-Z/a-z/ unless /^Make/' *
# rename '$_ .= ".bad"' *.f
# rename 'print "$_: "; s/foo/bar/ if <stdin> =~ /^y/i' *

$operation = shift;
for (@ARGV)
{
$previous = $_;
eval $operation;
die $@ if $@;
if ($previous ne $_)
{
if (-f $_)
{
warn "$0: $_ already exists\n";
}
else
{
rename($previous, $_)
|| warn "$0: can't rename $previous to $_: $!\n";
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top