How to use rename command with printf
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
As I was trying to fix the episode numbers of a TV series I toyed with the rename function and trying to pass the return from printf as the rename parameters, but could not do it. I tried just about every combination of
for j in $(seq 1 9);
do
rename 's/"$( - "`printf "%d" $j`" - )"/"$( - "`printf "0%d" $j`" - )"/g' *
done
that I could think of but nothing seemed to work. I ended up just doing it manually since I was only concerned with 1-9 in this case, so
rename 's/- 1 -/- 01 -/g' *
rename 's/- 2 -/- 02 -/g' *
...
Can someone point out why the above didn't work for future reference?
bash rename for printf
add a comment |Â
up vote
0
down vote
favorite
As I was trying to fix the episode numbers of a TV series I toyed with the rename function and trying to pass the return from printf as the rename parameters, but could not do it. I tried just about every combination of
for j in $(seq 1 9);
do
rename 's/"$( - "`printf "%d" $j`" - )"/"$( - "`printf "0%d" $j`" - )"/g' *
done
that I could think of but nothing seemed to work. I ended up just doing it manually since I was only concerned with 1-9 in this case, so
rename 's/- 1 -/- 01 -/g' *
rename 's/- 2 -/- 02 -/g' *
...
Can someone point out why the above didn't work for future reference?
bash rename for printf
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
As I was trying to fix the episode numbers of a TV series I toyed with the rename function and trying to pass the return from printf as the rename parameters, but could not do it. I tried just about every combination of
for j in $(seq 1 9);
do
rename 's/"$( - "`printf "%d" $j`" - )"/"$( - "`printf "0%d" $j`" - )"/g' *
done
that I could think of but nothing seemed to work. I ended up just doing it manually since I was only concerned with 1-9 in this case, so
rename 's/- 1 -/- 01 -/g' *
rename 's/- 2 -/- 02 -/g' *
...
Can someone point out why the above didn't work for future reference?
bash rename for printf
As I was trying to fix the episode numbers of a TV series I toyed with the rename function and trying to pass the return from printf as the rename parameters, but could not do it. I tried just about every combination of
for j in $(seq 1 9);
do
rename 's/"$( - "`printf "%d" $j`" - )"/"$( - "`printf "0%d" $j`" - )"/g' *
done
that I could think of but nothing seemed to work. I ended up just doing it manually since I was only concerned with 1-9 in this case, so
rename 's/- 1 -/- 01 -/g' *
rename 's/- 2 -/- 02 -/g' *
...
Can someone point out why the above didn't work for future reference?
bash rename for printf
asked Oct 27 '17 at 23:53
Darren Rodriguez
82
82
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
You are single quoting the entire expression, so no shell interpolation will happen, and Perl will see the most curious regular expression of "$( - "
printf "%d" $j" - )"
which is interpolated by Perl into something like
% perl -E 'say qq"$( - "`printf "%d" $j`" - )"'
"42 640 - "`printf "%d" `" - )"
%
because you asked Perl to interpolate the $(
variable which you can read about via perldoc -v '$('
. Suffice to say, this regular expression will probably not match your files. (And demonstrates one of the several pitfalls of attempting to embed multiple languages into one gloriously complicated string; there is really no need as Perl can do it all.)
A better approach would be to match and adjust what you are interested in, which here appears to be a number enclosed by hypens; assuming there is only one number in the filename simply match on that saving a backreference ([0-9]+)
and use the Perl sprintf
function to pad those numbers as appropriate (plus the important /e
flag to treat the right hand side as an expression so sprintf
actually gets called).
% touch "blah - "1..9" - blah"
% ls
blah - 1 - blah blah - 3 - blah blah - 5 - blah blah - 7 - blah blah - 9 - blah
blah - 2 - blah blah - 4 - blah blah - 6 - blah blah - 8 - blah
% rename 's/([0-9]+)/sprintf "%02d", $1/e' *
% ls
blah - 01 - blah blah - 04 - blah blah - 07 - blah
blah - 02 - blah blah - 05 - blah blah - 08 - blah
blah - 03 - blah blah - 06 - blah blah - 09 - blah
%
If there are numbers elsewhere in the filename, then make the regular expression account for that, perhaps something like
% rename 's/- ([0-9]+) -/sprintf "- %d -", $1/e' *
% ls
blah - 1 - blah blah - 3 - blah blah - 5 - blah blah - 7 - blah blah - 9 - blah
blah - 2 - blah blah - 4 - blah blah - 6 - blah blah - 8 - blah
%
To use regular expressions in rename
most effectively, see
- http://perl-begin.org/topics/regular-expressions/
- http://perldoc.perl.org/perlrequick.html
- http://perldoc.perl.org/perlretut.html
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You are single quoting the entire expression, so no shell interpolation will happen, and Perl will see the most curious regular expression of "$( - "
printf "%d" $j" - )"
which is interpolated by Perl into something like
% perl -E 'say qq"$( - "`printf "%d" $j`" - )"'
"42 640 - "`printf "%d" `" - )"
%
because you asked Perl to interpolate the $(
variable which you can read about via perldoc -v '$('
. Suffice to say, this regular expression will probably not match your files. (And demonstrates one of the several pitfalls of attempting to embed multiple languages into one gloriously complicated string; there is really no need as Perl can do it all.)
A better approach would be to match and adjust what you are interested in, which here appears to be a number enclosed by hypens; assuming there is only one number in the filename simply match on that saving a backreference ([0-9]+)
and use the Perl sprintf
function to pad those numbers as appropriate (plus the important /e
flag to treat the right hand side as an expression so sprintf
actually gets called).
% touch "blah - "1..9" - blah"
% ls
blah - 1 - blah blah - 3 - blah blah - 5 - blah blah - 7 - blah blah - 9 - blah
blah - 2 - blah blah - 4 - blah blah - 6 - blah blah - 8 - blah
% rename 's/([0-9]+)/sprintf "%02d", $1/e' *
% ls
blah - 01 - blah blah - 04 - blah blah - 07 - blah
blah - 02 - blah blah - 05 - blah blah - 08 - blah
blah - 03 - blah blah - 06 - blah blah - 09 - blah
%
If there are numbers elsewhere in the filename, then make the regular expression account for that, perhaps something like
% rename 's/- ([0-9]+) -/sprintf "- %d -", $1/e' *
% ls
blah - 1 - blah blah - 3 - blah blah - 5 - blah blah - 7 - blah blah - 9 - blah
blah - 2 - blah blah - 4 - blah blah - 6 - blah blah - 8 - blah
%
To use regular expressions in rename
most effectively, see
- http://perl-begin.org/topics/regular-expressions/
- http://perldoc.perl.org/perlrequick.html
- http://perldoc.perl.org/perlretut.html
add a comment |Â
up vote
3
down vote
accepted
You are single quoting the entire expression, so no shell interpolation will happen, and Perl will see the most curious regular expression of "$( - "
printf "%d" $j" - )"
which is interpolated by Perl into something like
% perl -E 'say qq"$( - "`printf "%d" $j`" - )"'
"42 640 - "`printf "%d" `" - )"
%
because you asked Perl to interpolate the $(
variable which you can read about via perldoc -v '$('
. Suffice to say, this regular expression will probably not match your files. (And demonstrates one of the several pitfalls of attempting to embed multiple languages into one gloriously complicated string; there is really no need as Perl can do it all.)
A better approach would be to match and adjust what you are interested in, which here appears to be a number enclosed by hypens; assuming there is only one number in the filename simply match on that saving a backreference ([0-9]+)
and use the Perl sprintf
function to pad those numbers as appropriate (plus the important /e
flag to treat the right hand side as an expression so sprintf
actually gets called).
% touch "blah - "1..9" - blah"
% ls
blah - 1 - blah blah - 3 - blah blah - 5 - blah blah - 7 - blah blah - 9 - blah
blah - 2 - blah blah - 4 - blah blah - 6 - blah blah - 8 - blah
% rename 's/([0-9]+)/sprintf "%02d", $1/e' *
% ls
blah - 01 - blah blah - 04 - blah blah - 07 - blah
blah - 02 - blah blah - 05 - blah blah - 08 - blah
blah - 03 - blah blah - 06 - blah blah - 09 - blah
%
If there are numbers elsewhere in the filename, then make the regular expression account for that, perhaps something like
% rename 's/- ([0-9]+) -/sprintf "- %d -", $1/e' *
% ls
blah - 1 - blah blah - 3 - blah blah - 5 - blah blah - 7 - blah blah - 9 - blah
blah - 2 - blah blah - 4 - blah blah - 6 - blah blah - 8 - blah
%
To use regular expressions in rename
most effectively, see
- http://perl-begin.org/topics/regular-expressions/
- http://perldoc.perl.org/perlrequick.html
- http://perldoc.perl.org/perlretut.html
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You are single quoting the entire expression, so no shell interpolation will happen, and Perl will see the most curious regular expression of "$( - "
printf "%d" $j" - )"
which is interpolated by Perl into something like
% perl -E 'say qq"$( - "`printf "%d" $j`" - )"'
"42 640 - "`printf "%d" `" - )"
%
because you asked Perl to interpolate the $(
variable which you can read about via perldoc -v '$('
. Suffice to say, this regular expression will probably not match your files. (And demonstrates one of the several pitfalls of attempting to embed multiple languages into one gloriously complicated string; there is really no need as Perl can do it all.)
A better approach would be to match and adjust what you are interested in, which here appears to be a number enclosed by hypens; assuming there is only one number in the filename simply match on that saving a backreference ([0-9]+)
and use the Perl sprintf
function to pad those numbers as appropriate (plus the important /e
flag to treat the right hand side as an expression so sprintf
actually gets called).
% touch "blah - "1..9" - blah"
% ls
blah - 1 - blah blah - 3 - blah blah - 5 - blah blah - 7 - blah blah - 9 - blah
blah - 2 - blah blah - 4 - blah blah - 6 - blah blah - 8 - blah
% rename 's/([0-9]+)/sprintf "%02d", $1/e' *
% ls
blah - 01 - blah blah - 04 - blah blah - 07 - blah
blah - 02 - blah blah - 05 - blah blah - 08 - blah
blah - 03 - blah blah - 06 - blah blah - 09 - blah
%
If there are numbers elsewhere in the filename, then make the regular expression account for that, perhaps something like
% rename 's/- ([0-9]+) -/sprintf "- %d -", $1/e' *
% ls
blah - 1 - blah blah - 3 - blah blah - 5 - blah blah - 7 - blah blah - 9 - blah
blah - 2 - blah blah - 4 - blah blah - 6 - blah blah - 8 - blah
%
To use regular expressions in rename
most effectively, see
- http://perl-begin.org/topics/regular-expressions/
- http://perldoc.perl.org/perlrequick.html
- http://perldoc.perl.org/perlretut.html
You are single quoting the entire expression, so no shell interpolation will happen, and Perl will see the most curious regular expression of "$( - "
printf "%d" $j" - )"
which is interpolated by Perl into something like
% perl -E 'say qq"$( - "`printf "%d" $j`" - )"'
"42 640 - "`printf "%d" `" - )"
%
because you asked Perl to interpolate the $(
variable which you can read about via perldoc -v '$('
. Suffice to say, this regular expression will probably not match your files. (And demonstrates one of the several pitfalls of attempting to embed multiple languages into one gloriously complicated string; there is really no need as Perl can do it all.)
A better approach would be to match and adjust what you are interested in, which here appears to be a number enclosed by hypens; assuming there is only one number in the filename simply match on that saving a backreference ([0-9]+)
and use the Perl sprintf
function to pad those numbers as appropriate (plus the important /e
flag to treat the right hand side as an expression so sprintf
actually gets called).
% touch "blah - "1..9" - blah"
% ls
blah - 1 - blah blah - 3 - blah blah - 5 - blah blah - 7 - blah blah - 9 - blah
blah - 2 - blah blah - 4 - blah blah - 6 - blah blah - 8 - blah
% rename 's/([0-9]+)/sprintf "%02d", $1/e' *
% ls
blah - 01 - blah blah - 04 - blah blah - 07 - blah
blah - 02 - blah blah - 05 - blah blah - 08 - blah
blah - 03 - blah blah - 06 - blah blah - 09 - blah
%
If there are numbers elsewhere in the filename, then make the regular expression account for that, perhaps something like
% rename 's/- ([0-9]+) -/sprintf "- %d -", $1/e' *
% ls
blah - 1 - blah blah - 3 - blah blah - 5 - blah blah - 7 - blah blah - 9 - blah
blah - 2 - blah blah - 4 - blah blah - 6 - blah blah - 8 - blah
%
To use regular expressions in rename
most effectively, see
- http://perl-begin.org/topics/regular-expressions/
- http://perldoc.perl.org/perlrequick.html
- http://perldoc.perl.org/perlretut.html
edited Oct 28 '17 at 1:07
answered Oct 28 '17 at 0:17
thrig
22.7k12853
22.7k12853
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f400983%2fhow-to-use-rename-command-with-printf%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password