Suppose I have files named like GATES, Bill.jpg and I want to rename them all to Bill Gates.jpg. I can capture the two words
rename 's/^(.*?), (.*?)\./$2 $1\./g' *
To change a case there are some Perl's functions:
$lower = lc("aBcDe"); # $lower is assigned "abcde"
$upper = uc("aBcDe"); # $upper is assigned "ABCDE"
$lower = lcfirst("HELLO"); # $lower is assigned "hELLO"
$upper = ucfirst("hello"); # $upper is assigned "Hello"
I tried to make use of them:
rename 's/^(.*?), (.*?)\./$2 ucfirst($1)\./g' *
But it doesn't work.
You need to add the "e" (eval) flag to the end of the regular expression, otherwise the function won't be executed. This means that the entire second part of the s/// expression has to be a valid Perl expression (instead of a valid string):
rename 's/^(.*?), (.*?)\./"$2 " . ucfirst(lc($1)) . "."/ge' *
(also note the extra space inside the string with $2)
More information on this flag can be found in the perlre documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With