Rename file name between two points

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I am looking for a way to rename a batch of videos by remove text between two points. Specifically I would like to remove everything after the 6th character, to the last "LabelMe--".



0001--LabelMe--A005_01241110_C001--LabelMe-- --Interview--Man in library.mov



0002--LabelMe--A005_01241110_C002--LabelMe-- --Broll--Man looking at books.mov



0003--LabelMe--A005_01241111_C003--LabelMe-- --Broll--messed up--LabelMe-- --Broll--Sitting at table.mov



I am trying to do this using Apple's Automator Services. Right-click on a bunch of files and select the service to clean up the name. I found one piece of code but am not sure I am using it correctly.



for f in "$@"
do
echo sed -e 's/(--LabelMe).*(LabelMe--)/12/'
done


enter image description here







share|improve this question


























    up vote
    0
    down vote

    favorite












    I am looking for a way to rename a batch of videos by remove text between two points. Specifically I would like to remove everything after the 6th character, to the last "LabelMe--".



    0001--LabelMe--A005_01241110_C001--LabelMe-- --Interview--Man in library.mov



    0002--LabelMe--A005_01241110_C002--LabelMe-- --Broll--Man looking at books.mov



    0003--LabelMe--A005_01241111_C003--LabelMe-- --Broll--messed up--LabelMe-- --Broll--Sitting at table.mov



    I am trying to do this using Apple's Automator Services. Right-click on a bunch of files and select the service to clean up the name. I found one piece of code but am not sure I am using it correctly.



    for f in "$@"
    do
    echo sed -e 's/(--LabelMe).*(LabelMe--)/12/'
    done


    enter image description here







    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am looking for a way to rename a batch of videos by remove text between two points. Specifically I would like to remove everything after the 6th character, to the last "LabelMe--".



      0001--LabelMe--A005_01241110_C001--LabelMe-- --Interview--Man in library.mov



      0002--LabelMe--A005_01241110_C002--LabelMe-- --Broll--Man looking at books.mov



      0003--LabelMe--A005_01241111_C003--LabelMe-- --Broll--messed up--LabelMe-- --Broll--Sitting at table.mov



      I am trying to do this using Apple's Automator Services. Right-click on a bunch of files and select the service to clean up the name. I found one piece of code but am not sure I am using it correctly.



      for f in "$@"
      do
      echo sed -e 's/(--LabelMe).*(LabelMe--)/12/'
      done


      enter image description here







      share|improve this question














      I am looking for a way to rename a batch of videos by remove text between two points. Specifically I would like to remove everything after the 6th character, to the last "LabelMe--".



      0001--LabelMe--A005_01241110_C001--LabelMe-- --Interview--Man in library.mov



      0002--LabelMe--A005_01241110_C002--LabelMe-- --Broll--Man looking at books.mov



      0003--LabelMe--A005_01241111_C003--LabelMe-- --Broll--messed up--LabelMe-- --Broll--Sitting at table.mov



      I am trying to do this using Apple's Automator Services. Right-click on a bunch of files and select the service to clean up the name. I found one piece of code but am not sure I am using it correctly.



      for f in "$@"
      do
      echo sed -e 's/(--LabelMe).*(LabelMe--)/12/'
      done


      enter image description here









      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 9 at 8:55









      ilkkachu

      49.6k673137




      49.6k673137










      asked Feb 8 at 23:09









      Chad

      31




      31




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          There's a bunch of questions on this site about renaming files using shell loops like that, and a couple about using sed for that. (e.g. this, this)



          You'll need to pass the filename to sed with a pipe and use command substitution to catch the result, so something like this:



          mv "$f" "$(echo "$f" | sed -e '...' )"


          The sed command you used needs a bit of fixing: For one, you've quoted the opening parenthesis but not the closing ones, so the grouping won't work. Also, I'm not sure why you'd like to put the LabelMe snippets back to the file name. If you want to remove them instead, just skip the grouping and the N references:



          's/--LabelMe.*LabelMe--//'


          So in full:



          for f in "$@"
          do
          mv "$f" "$(echo "$f" | sed -e 's/--LabelMe.*LabelMe--//' )"
          done


          Another way would be to use the shell's parameter substitution to do the same. The substitution $par/pattern/replacement is nonstandard, but supported in many shells:



          for f in "$@"
          do
          mv "$f" "$f/--LabelMe*LabelMe--/"
          done


          (note that's not a regex, so the syntax for "anything" is just *, and not .*)






          share|improve this answer




















          • That is amazing! Worked perfectly. This is a great way to start my day. Thank you so much.
            – Chad
            Feb 9 at 14:51

















          up vote
          1
          down vote













          Use (perl) rename. I don't know about Apple, but Linux distros mostly ship with two versions of rename. Perl rename is the more powerful one.



          Firstly, run the command with the -n flag, which does a "dry-run", and makes no changes.



          $ rename 's/(.6).*LabelMe--/$1/' 000* -n
          0001--LabelMe--A005_01241110_C001--LabelMe-- --Interview--Man in library.mov -> 1110_C --Interview--Man in library.mov
          0002--LabelMe--A005_01241110_C002--LabelMe-- --Broll--Man looking at books.mov -> 1110_C --Broll--Man looking at books.mov
          0003--LabelMe--A005_01241111_C003--LabelMe-- --Broll--messed up--LabelMe-- --Broll--Sitting at table.mov -> messed --Broll--Sitting at table.mov


          Once you are happy with the result, run it for real, i.e. rename 's/(.6).*LabelMe--/$1/' 000*



          Explanation




          • rename 's/foo/bar/' 000*: This format will search for the foo regex and replace it with bar. It will operate on all files matching the glob 000*.


          • (.6).*LabelMe--: This is the regex that you are searching for. You are looking for six of any character .6, which you put into a capturing group (.6). After this, you can have any characters .*, before finding LabelMe--.


          • $1: This is what you will replace the above string with. $1 refers to the contents of the matching group (.6) above. The rest will not be replaced, i.e. you delete everything after the first six character up to and including LabelMe--.





          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%2f422927%2frename-file-name-between-two-points%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            There's a bunch of questions on this site about renaming files using shell loops like that, and a couple about using sed for that. (e.g. this, this)



            You'll need to pass the filename to sed with a pipe and use command substitution to catch the result, so something like this:



            mv "$f" "$(echo "$f" | sed -e '...' )"


            The sed command you used needs a bit of fixing: For one, you've quoted the opening parenthesis but not the closing ones, so the grouping won't work. Also, I'm not sure why you'd like to put the LabelMe snippets back to the file name. If you want to remove them instead, just skip the grouping and the N references:



            's/--LabelMe.*LabelMe--//'


            So in full:



            for f in "$@"
            do
            mv "$f" "$(echo "$f" | sed -e 's/--LabelMe.*LabelMe--//' )"
            done


            Another way would be to use the shell's parameter substitution to do the same. The substitution $par/pattern/replacement is nonstandard, but supported in many shells:



            for f in "$@"
            do
            mv "$f" "$f/--LabelMe*LabelMe--/"
            done


            (note that's not a regex, so the syntax for "anything" is just *, and not .*)






            share|improve this answer




















            • That is amazing! Worked perfectly. This is a great way to start my day. Thank you so much.
              – Chad
              Feb 9 at 14:51














            up vote
            0
            down vote



            accepted










            There's a bunch of questions on this site about renaming files using shell loops like that, and a couple about using sed for that. (e.g. this, this)



            You'll need to pass the filename to sed with a pipe and use command substitution to catch the result, so something like this:



            mv "$f" "$(echo "$f" | sed -e '...' )"


            The sed command you used needs a bit of fixing: For one, you've quoted the opening parenthesis but not the closing ones, so the grouping won't work. Also, I'm not sure why you'd like to put the LabelMe snippets back to the file name. If you want to remove them instead, just skip the grouping and the N references:



            's/--LabelMe.*LabelMe--//'


            So in full:



            for f in "$@"
            do
            mv "$f" "$(echo "$f" | sed -e 's/--LabelMe.*LabelMe--//' )"
            done


            Another way would be to use the shell's parameter substitution to do the same. The substitution $par/pattern/replacement is nonstandard, but supported in many shells:



            for f in "$@"
            do
            mv "$f" "$f/--LabelMe*LabelMe--/"
            done


            (note that's not a regex, so the syntax for "anything" is just *, and not .*)






            share|improve this answer




















            • That is amazing! Worked perfectly. This is a great way to start my day. Thank you so much.
              – Chad
              Feb 9 at 14:51












            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            There's a bunch of questions on this site about renaming files using shell loops like that, and a couple about using sed for that. (e.g. this, this)



            You'll need to pass the filename to sed with a pipe and use command substitution to catch the result, so something like this:



            mv "$f" "$(echo "$f" | sed -e '...' )"


            The sed command you used needs a bit of fixing: For one, you've quoted the opening parenthesis but not the closing ones, so the grouping won't work. Also, I'm not sure why you'd like to put the LabelMe snippets back to the file name. If you want to remove them instead, just skip the grouping and the N references:



            's/--LabelMe.*LabelMe--//'


            So in full:



            for f in "$@"
            do
            mv "$f" "$(echo "$f" | sed -e 's/--LabelMe.*LabelMe--//' )"
            done


            Another way would be to use the shell's parameter substitution to do the same. The substitution $par/pattern/replacement is nonstandard, but supported in many shells:



            for f in "$@"
            do
            mv "$f" "$f/--LabelMe*LabelMe--/"
            done


            (note that's not a regex, so the syntax for "anything" is just *, and not .*)






            share|improve this answer












            There's a bunch of questions on this site about renaming files using shell loops like that, and a couple about using sed for that. (e.g. this, this)



            You'll need to pass the filename to sed with a pipe and use command substitution to catch the result, so something like this:



            mv "$f" "$(echo "$f" | sed -e '...' )"


            The sed command you used needs a bit of fixing: For one, you've quoted the opening parenthesis but not the closing ones, so the grouping won't work. Also, I'm not sure why you'd like to put the LabelMe snippets back to the file name. If you want to remove them instead, just skip the grouping and the N references:



            's/--LabelMe.*LabelMe--//'


            So in full:



            for f in "$@"
            do
            mv "$f" "$(echo "$f" | sed -e 's/--LabelMe.*LabelMe--//' )"
            done


            Another way would be to use the shell's parameter substitution to do the same. The substitution $par/pattern/replacement is nonstandard, but supported in many shells:



            for f in "$@"
            do
            mv "$f" "$f/--LabelMe*LabelMe--/"
            done


            (note that's not a regex, so the syntax for "anything" is just *, and not .*)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 9 at 9:20









            ilkkachu

            49.6k673137




            49.6k673137











            • That is amazing! Worked perfectly. This is a great way to start my day. Thank you so much.
              – Chad
              Feb 9 at 14:51
















            • That is amazing! Worked perfectly. This is a great way to start my day. Thank you so much.
              – Chad
              Feb 9 at 14:51















            That is amazing! Worked perfectly. This is a great way to start my day. Thank you so much.
            – Chad
            Feb 9 at 14:51




            That is amazing! Worked perfectly. This is a great way to start my day. Thank you so much.
            – Chad
            Feb 9 at 14:51












            up vote
            1
            down vote













            Use (perl) rename. I don't know about Apple, but Linux distros mostly ship with two versions of rename. Perl rename is the more powerful one.



            Firstly, run the command with the -n flag, which does a "dry-run", and makes no changes.



            $ rename 's/(.6).*LabelMe--/$1/' 000* -n
            0001--LabelMe--A005_01241110_C001--LabelMe-- --Interview--Man in library.mov -> 1110_C --Interview--Man in library.mov
            0002--LabelMe--A005_01241110_C002--LabelMe-- --Broll--Man looking at books.mov -> 1110_C --Broll--Man looking at books.mov
            0003--LabelMe--A005_01241111_C003--LabelMe-- --Broll--messed up--LabelMe-- --Broll--Sitting at table.mov -> messed --Broll--Sitting at table.mov


            Once you are happy with the result, run it for real, i.e. rename 's/(.6).*LabelMe--/$1/' 000*



            Explanation




            • rename 's/foo/bar/' 000*: This format will search for the foo regex and replace it with bar. It will operate on all files matching the glob 000*.


            • (.6).*LabelMe--: This is the regex that you are searching for. You are looking for six of any character .6, which you put into a capturing group (.6). After this, you can have any characters .*, before finding LabelMe--.


            • $1: This is what you will replace the above string with. $1 refers to the contents of the matching group (.6) above. The rest will not be replaced, i.e. you delete everything after the first six character up to and including LabelMe--.





            share|improve this answer
























              up vote
              1
              down vote













              Use (perl) rename. I don't know about Apple, but Linux distros mostly ship with two versions of rename. Perl rename is the more powerful one.



              Firstly, run the command with the -n flag, which does a "dry-run", and makes no changes.



              $ rename 's/(.6).*LabelMe--/$1/' 000* -n
              0001--LabelMe--A005_01241110_C001--LabelMe-- --Interview--Man in library.mov -> 1110_C --Interview--Man in library.mov
              0002--LabelMe--A005_01241110_C002--LabelMe-- --Broll--Man looking at books.mov -> 1110_C --Broll--Man looking at books.mov
              0003--LabelMe--A005_01241111_C003--LabelMe-- --Broll--messed up--LabelMe-- --Broll--Sitting at table.mov -> messed --Broll--Sitting at table.mov


              Once you are happy with the result, run it for real, i.e. rename 's/(.6).*LabelMe--/$1/' 000*



              Explanation




              • rename 's/foo/bar/' 000*: This format will search for the foo regex and replace it with bar. It will operate on all files matching the glob 000*.


              • (.6).*LabelMe--: This is the regex that you are searching for. You are looking for six of any character .6, which you put into a capturing group (.6). After this, you can have any characters .*, before finding LabelMe--.


              • $1: This is what you will replace the above string with. $1 refers to the contents of the matching group (.6) above. The rest will not be replaced, i.e. you delete everything after the first six character up to and including LabelMe--.





              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                Use (perl) rename. I don't know about Apple, but Linux distros mostly ship with two versions of rename. Perl rename is the more powerful one.



                Firstly, run the command with the -n flag, which does a "dry-run", and makes no changes.



                $ rename 's/(.6).*LabelMe--/$1/' 000* -n
                0001--LabelMe--A005_01241110_C001--LabelMe-- --Interview--Man in library.mov -> 1110_C --Interview--Man in library.mov
                0002--LabelMe--A005_01241110_C002--LabelMe-- --Broll--Man looking at books.mov -> 1110_C --Broll--Man looking at books.mov
                0003--LabelMe--A005_01241111_C003--LabelMe-- --Broll--messed up--LabelMe-- --Broll--Sitting at table.mov -> messed --Broll--Sitting at table.mov


                Once you are happy with the result, run it for real, i.e. rename 's/(.6).*LabelMe--/$1/' 000*



                Explanation




                • rename 's/foo/bar/' 000*: This format will search for the foo regex and replace it with bar. It will operate on all files matching the glob 000*.


                • (.6).*LabelMe--: This is the regex that you are searching for. You are looking for six of any character .6, which you put into a capturing group (.6). After this, you can have any characters .*, before finding LabelMe--.


                • $1: This is what you will replace the above string with. $1 refers to the contents of the matching group (.6) above. The rest will not be replaced, i.e. you delete everything after the first six character up to and including LabelMe--.





                share|improve this answer












                Use (perl) rename. I don't know about Apple, but Linux distros mostly ship with two versions of rename. Perl rename is the more powerful one.



                Firstly, run the command with the -n flag, which does a "dry-run", and makes no changes.



                $ rename 's/(.6).*LabelMe--/$1/' 000* -n
                0001--LabelMe--A005_01241110_C001--LabelMe-- --Interview--Man in library.mov -> 1110_C --Interview--Man in library.mov
                0002--LabelMe--A005_01241110_C002--LabelMe-- --Broll--Man looking at books.mov -> 1110_C --Broll--Man looking at books.mov
                0003--LabelMe--A005_01241111_C003--LabelMe-- --Broll--messed up--LabelMe-- --Broll--Sitting at table.mov -> messed --Broll--Sitting at table.mov


                Once you are happy with the result, run it for real, i.e. rename 's/(.6).*LabelMe--/$1/' 000*



                Explanation




                • rename 's/foo/bar/' 000*: This format will search for the foo regex and replace it with bar. It will operate on all files matching the glob 000*.


                • (.6).*LabelMe--: This is the regex that you are searching for. You are looking for six of any character .6, which you put into a capturing group (.6). After this, you can have any characters .*, before finding LabelMe--.


                • $1: This is what you will replace the above string with. $1 refers to the contents of the matching group (.6) above. The rest will not be replaced, i.e. you delete everything after the first six character up to and including LabelMe--.






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 8 at 23:16









                Sparhawk

                8,35863487




                8,35863487






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422927%2frename-file-name-between-two-points%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?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?