How to use rename command with printf

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question
























    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?







    share|improve this question






















      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?







      share|improve this question












      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?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 27 '17 at 23:53









      Darren Rodriguez

      82




      82




















          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





          share|improve this answer






















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            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






























            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





            share|improve this answer


























              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





              share|improve this answer
























                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





                share|improve this answer














                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






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Oct 28 '17 at 1:07

























                answered Oct 28 '17 at 0:17









                thrig

                22.7k12853




                22.7k12853



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    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













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay