Script to remove string up to first number












-2















I need to write a script that will look through a folder and strip out the text from a string of an image.



image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
image-w-inch-bob-bob-bob-bob-8820-AV6.jpg


I need this to be renamed to



8820-AV1.jpg
8820-AV2.jpg
8820-AV3.jpg
8820-AV4.jpg
8820-AV5.jpg
8820-AV6.jpg


Does anyone know of a script like this?










share|improve this question

























  • The first number is 15. Please specify the constraints more precisely.

    – choroba
    Nov 21 '18 at 22:18











  • Updated @choroba

    – WebDevB
    Nov 21 '18 at 22:23











  • Your example data doesn't make clear where you want to cut. Should de target have 8.3 characters, start with the first digit, start with the second-last minus sign, removing everyting until the last bob-, remove leading minus signs after removing lowercase characters before the .jpg, or some other magic? What do you want when there is a space in the filename ?

    – Walter A
    Nov 21 '18 at 22:58
















-2















I need to write a script that will look through a folder and strip out the text from a string of an image.



image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
image-w-inch-bob-bob-bob-bob-8820-AV6.jpg


I need this to be renamed to



8820-AV1.jpg
8820-AV2.jpg
8820-AV3.jpg
8820-AV4.jpg
8820-AV5.jpg
8820-AV6.jpg


Does anyone know of a script like this?










share|improve this question

























  • The first number is 15. Please specify the constraints more precisely.

    – choroba
    Nov 21 '18 at 22:18











  • Updated @choroba

    – WebDevB
    Nov 21 '18 at 22:23











  • Your example data doesn't make clear where you want to cut. Should de target have 8.3 characters, start with the first digit, start with the second-last minus sign, removing everyting until the last bob-, remove leading minus signs after removing lowercase characters before the .jpg, or some other magic? What do you want when there is a space in the filename ?

    – Walter A
    Nov 21 '18 at 22:58














-2












-2








-2








I need to write a script that will look through a folder and strip out the text from a string of an image.



image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
image-w-inch-bob-bob-bob-bob-8820-AV6.jpg


I need this to be renamed to



8820-AV1.jpg
8820-AV2.jpg
8820-AV3.jpg
8820-AV4.jpg
8820-AV5.jpg
8820-AV6.jpg


Does anyone know of a script like this?










share|improve this question
















I need to write a script that will look through a folder and strip out the text from a string of an image.



image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
image-w-inch-bob-bob-bob-bob-8820-AV6.jpg


I need this to be renamed to



8820-AV1.jpg
8820-AV2.jpg
8820-AV3.jpg
8820-AV4.jpg
8820-AV5.jpg
8820-AV6.jpg


Does anyone know of a script like this?







python linux bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 22:22







WebDevB

















asked Nov 21 '18 at 22:16









WebDevBWebDevB

1952219




1952219













  • The first number is 15. Please specify the constraints more precisely.

    – choroba
    Nov 21 '18 at 22:18











  • Updated @choroba

    – WebDevB
    Nov 21 '18 at 22:23











  • Your example data doesn't make clear where you want to cut. Should de target have 8.3 characters, start with the first digit, start with the second-last minus sign, removing everyting until the last bob-, remove leading minus signs after removing lowercase characters before the .jpg, or some other magic? What do you want when there is a space in the filename ?

    – Walter A
    Nov 21 '18 at 22:58



















  • The first number is 15. Please specify the constraints more precisely.

    – choroba
    Nov 21 '18 at 22:18











  • Updated @choroba

    – WebDevB
    Nov 21 '18 at 22:23











  • Your example data doesn't make clear where you want to cut. Should de target have 8.3 characters, start with the first digit, start with the second-last minus sign, removing everyting until the last bob-, remove leading minus signs after removing lowercase characters before the .jpg, or some other magic? What do you want when there is a space in the filename ?

    – Walter A
    Nov 21 '18 at 22:58

















The first number is 15. Please specify the constraints more precisely.

– choroba
Nov 21 '18 at 22:18





The first number is 15. Please specify the constraints more precisely.

– choroba
Nov 21 '18 at 22:18













Updated @choroba

– WebDevB
Nov 21 '18 at 22:23





Updated @choroba

– WebDevB
Nov 21 '18 at 22:23













Your example data doesn't make clear where you want to cut. Should de target have 8.3 characters, start with the first digit, start with the second-last minus sign, removing everyting until the last bob-, remove leading minus signs after removing lowercase characters before the .jpg, or some other magic? What do you want when there is a space in the filename ?

– Walter A
Nov 21 '18 at 22:58





Your example data doesn't make clear where you want to cut. Should de target have 8.3 characters, start with the first digit, start with the second-last minus sign, removing everyting until the last bob-, remove leading minus signs after removing lowercase characters before the .jpg, or some other magic? What do you want when there is a space in the filename ?

– Walter A
Nov 21 '18 at 22:58












5 Answers
5






active

oldest

votes


















1














The most natural way would be to use regular expressions.



Here's an example of a python implementation:



import re
s = 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg'
capture_from_first_digit_re = re.compile('D*(d.*)')
print(capture_from_first_digit_re .findall(s)[0])


You can use this as you see fit in your context.





Short explanation:




  • D - anything except a digit

  • D* - keep going as long as you don't hit a digit

  • d - any digit

  • .* - anything at all

  • (###) - capture the matching ###


Putting it together: capture everything after the first digit you find.






share|improve this answer































    0














    If you're just trying to strip out all text before the first digit is found in the filename, something along these lines with a relatively simple regex should work in python:



    import os
    import re
    # replace with the path to your file:
    path = 'test/'
    for filename in os.listdir(path):
    os.rename(os.path.join(path, filename),
    os.path.join(path, re.search('d.*',filename).group()))


    We can create a test file just for fun to see that it works:



    import os
    import re

    # list out all the filenames to put into our test directory
    l=['image-w-inch-bob-bob-bob-bob-8820-AV1.jpg',
    'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg',
    'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg',
    'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg',
    'image-w-inch-bob-bob-bob-bob-8820-AV5.jpg',
    'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg']

    # Create Directory
    os.mkdir('test')

    # add in all the files
    for f in l:
    open(f'test/{f}','a').close()

    # All the files are there
    >>> os.listdir('test')
    ['image-w-inch-bob-bob-bob-bob-8820-AV5.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg']

    # rename with the loop provided above:

    path = 'test/'
    for filename in os.listdir(path):
    os.rename(os.path.join(path, filename),
    os.path.join(path, re.search('d.*',filename).group()))

    # all the filenames have changed
    >>> os.listdir('test')
    ['8820-AV1.jpg', '8820-AV3.jpg', '8820-AV2.jpg', '8820-AV6.jpg', '8820-AV5.jpg', '8820-AV4.jpg']





    share|improve this answer

































      0














      The easiest way to do this would be to locate the python script inside the folder the files are located. Assuming that all the filenames have the same amount of characters before the first number, then you'd need something like:



      import os
      for file in os.listdir('.'):
      if '.py' not in file: #not the python script
      os.rename(file, file[indexOfFirstNum:])


      I haven't tested the script so try it first and modify accordingly before running it in the real folder.



      EDIT: Refer to shlomif's answer if you want the script to be more general. Regular expressions are useful for finding and matching patterns in strings.






      share|improve this answer

































        0














        Here is the Perl one liner solution:



        $ ls -l
        total 0
        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV6.jpg

        $ perl -ne ' BEGIN { foreach $f (glob("image-w*.jpg")) { $newf=$f, $newf=~s/(.[^d]*)-(d+)(.*)/23/g; rename $f, "$newf" } exit } '

        $ ls -l
        total 0
        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV1.jpg
        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV2.jpg
        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV3.jpg
        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV4.jpg
        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV5.jpg
        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV6.jpg

        $





        share|improve this answer































          0














          You most likely can do this with rename if your distro provides this command. For example:



          $ rename 'image-w-inch-bob-bob-bob-bob-' '' image-w-"


          or with PCRE Regex as the expression:



          $ rename 's/image-w-inch-bob-bob-bob-bob-//' image-w-*


          rename can batch rename a set of files according to a pattern match, including by regular expression. Note that the are several versions of rename which accepts different syntax, you should consult your local man and/or the command's help page to determine which version you are using and the precise syntax to use.






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421214%2fscript-to-remove-string-up-to-first-number%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The most natural way would be to use regular expressions.



            Here's an example of a python implementation:



            import re
            s = 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg'
            capture_from_first_digit_re = re.compile('D*(d.*)')
            print(capture_from_first_digit_re .findall(s)[0])


            You can use this as you see fit in your context.





            Short explanation:




            • D - anything except a digit

            • D* - keep going as long as you don't hit a digit

            • d - any digit

            • .* - anything at all

            • (###) - capture the matching ###


            Putting it together: capture everything after the first digit you find.






            share|improve this answer




























              1














              The most natural way would be to use regular expressions.



              Here's an example of a python implementation:



              import re
              s = 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg'
              capture_from_first_digit_re = re.compile('D*(d.*)')
              print(capture_from_first_digit_re .findall(s)[0])


              You can use this as you see fit in your context.





              Short explanation:




              • D - anything except a digit

              • D* - keep going as long as you don't hit a digit

              • d - any digit

              • .* - anything at all

              • (###) - capture the matching ###


              Putting it together: capture everything after the first digit you find.






              share|improve this answer


























                1












                1








                1







                The most natural way would be to use regular expressions.



                Here's an example of a python implementation:



                import re
                s = 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg'
                capture_from_first_digit_re = re.compile('D*(d.*)')
                print(capture_from_first_digit_re .findall(s)[0])


                You can use this as you see fit in your context.





                Short explanation:




                • D - anything except a digit

                • D* - keep going as long as you don't hit a digit

                • d - any digit

                • .* - anything at all

                • (###) - capture the matching ###


                Putting it together: capture everything after the first digit you find.






                share|improve this answer













                The most natural way would be to use regular expressions.



                Here's an example of a python implementation:



                import re
                s = 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg'
                capture_from_first_digit_re = re.compile('D*(d.*)')
                print(capture_from_first_digit_re .findall(s)[0])


                You can use this as you see fit in your context.





                Short explanation:




                • D - anything except a digit

                • D* - keep going as long as you don't hit a digit

                • d - any digit

                • .* - anything at all

                • (###) - capture the matching ###


                Putting it together: capture everything after the first digit you find.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 22:33









                ShlomiFShlomiF

                855410




                855410

























                    0














                    If you're just trying to strip out all text before the first digit is found in the filename, something along these lines with a relatively simple regex should work in python:



                    import os
                    import re
                    # replace with the path to your file:
                    path = 'test/'
                    for filename in os.listdir(path):
                    os.rename(os.path.join(path, filename),
                    os.path.join(path, re.search('d.*',filename).group()))


                    We can create a test file just for fun to see that it works:



                    import os
                    import re

                    # list out all the filenames to put into our test directory
                    l=['image-w-inch-bob-bob-bob-bob-8820-AV1.jpg',
                    'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg',
                    'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg',
                    'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg',
                    'image-w-inch-bob-bob-bob-bob-8820-AV5.jpg',
                    'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg']

                    # Create Directory
                    os.mkdir('test')

                    # add in all the files
                    for f in l:
                    open(f'test/{f}','a').close()

                    # All the files are there
                    >>> os.listdir('test')
                    ['image-w-inch-bob-bob-bob-bob-8820-AV5.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg']

                    # rename with the loop provided above:

                    path = 'test/'
                    for filename in os.listdir(path):
                    os.rename(os.path.join(path, filename),
                    os.path.join(path, re.search('d.*',filename).group()))

                    # all the filenames have changed
                    >>> os.listdir('test')
                    ['8820-AV1.jpg', '8820-AV3.jpg', '8820-AV2.jpg', '8820-AV6.jpg', '8820-AV5.jpg', '8820-AV4.jpg']





                    share|improve this answer






























                      0














                      If you're just trying to strip out all text before the first digit is found in the filename, something along these lines with a relatively simple regex should work in python:



                      import os
                      import re
                      # replace with the path to your file:
                      path = 'test/'
                      for filename in os.listdir(path):
                      os.rename(os.path.join(path, filename),
                      os.path.join(path, re.search('d.*',filename).group()))


                      We can create a test file just for fun to see that it works:



                      import os
                      import re

                      # list out all the filenames to put into our test directory
                      l=['image-w-inch-bob-bob-bob-bob-8820-AV1.jpg',
                      'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg',
                      'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg',
                      'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg',
                      'image-w-inch-bob-bob-bob-bob-8820-AV5.jpg',
                      'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg']

                      # Create Directory
                      os.mkdir('test')

                      # add in all the files
                      for f in l:
                      open(f'test/{f}','a').close()

                      # All the files are there
                      >>> os.listdir('test')
                      ['image-w-inch-bob-bob-bob-bob-8820-AV5.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg']

                      # rename with the loop provided above:

                      path = 'test/'
                      for filename in os.listdir(path):
                      os.rename(os.path.join(path, filename),
                      os.path.join(path, re.search('d.*',filename).group()))

                      # all the filenames have changed
                      >>> os.listdir('test')
                      ['8820-AV1.jpg', '8820-AV3.jpg', '8820-AV2.jpg', '8820-AV6.jpg', '8820-AV5.jpg', '8820-AV4.jpg']





                      share|improve this answer




























                        0












                        0








                        0







                        If you're just trying to strip out all text before the first digit is found in the filename, something along these lines with a relatively simple regex should work in python:



                        import os
                        import re
                        # replace with the path to your file:
                        path = 'test/'
                        for filename in os.listdir(path):
                        os.rename(os.path.join(path, filename),
                        os.path.join(path, re.search('d.*',filename).group()))


                        We can create a test file just for fun to see that it works:



                        import os
                        import re

                        # list out all the filenames to put into our test directory
                        l=['image-w-inch-bob-bob-bob-bob-8820-AV1.jpg',
                        'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg',
                        'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg',
                        'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg',
                        'image-w-inch-bob-bob-bob-bob-8820-AV5.jpg',
                        'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg']

                        # Create Directory
                        os.mkdir('test')

                        # add in all the files
                        for f in l:
                        open(f'test/{f}','a').close()

                        # All the files are there
                        >>> os.listdir('test')
                        ['image-w-inch-bob-bob-bob-bob-8820-AV5.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg']

                        # rename with the loop provided above:

                        path = 'test/'
                        for filename in os.listdir(path):
                        os.rename(os.path.join(path, filename),
                        os.path.join(path, re.search('d.*',filename).group()))

                        # all the filenames have changed
                        >>> os.listdir('test')
                        ['8820-AV1.jpg', '8820-AV3.jpg', '8820-AV2.jpg', '8820-AV6.jpg', '8820-AV5.jpg', '8820-AV4.jpg']





                        share|improve this answer















                        If you're just trying to strip out all text before the first digit is found in the filename, something along these lines with a relatively simple regex should work in python:



                        import os
                        import re
                        # replace with the path to your file:
                        path = 'test/'
                        for filename in os.listdir(path):
                        os.rename(os.path.join(path, filename),
                        os.path.join(path, re.search('d.*',filename).group()))


                        We can create a test file just for fun to see that it works:



                        import os
                        import re

                        # list out all the filenames to put into our test directory
                        l=['image-w-inch-bob-bob-bob-bob-8820-AV1.jpg',
                        'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg',
                        'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg',
                        'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg',
                        'image-w-inch-bob-bob-bob-bob-8820-AV5.jpg',
                        'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg']

                        # Create Directory
                        os.mkdir('test')

                        # add in all the files
                        for f in l:
                        open(f'test/{f}','a').close()

                        # All the files are there
                        >>> os.listdir('test')
                        ['image-w-inch-bob-bob-bob-bob-8820-AV5.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg']

                        # rename with the loop provided above:

                        path = 'test/'
                        for filename in os.listdir(path):
                        os.rename(os.path.join(path, filename),
                        os.path.join(path, re.search('d.*',filename).group()))

                        # all the filenames have changed
                        >>> os.listdir('test')
                        ['8820-AV1.jpg', '8820-AV3.jpg', '8820-AV2.jpg', '8820-AV6.jpg', '8820-AV5.jpg', '8820-AV4.jpg']






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Nov 21 '18 at 22:39

























                        answered Nov 21 '18 at 22:32









                        sacuLsacuL

                        30.6k41943




                        30.6k41943























                            0














                            The easiest way to do this would be to locate the python script inside the folder the files are located. Assuming that all the filenames have the same amount of characters before the first number, then you'd need something like:



                            import os
                            for file in os.listdir('.'):
                            if '.py' not in file: #not the python script
                            os.rename(file, file[indexOfFirstNum:])


                            I haven't tested the script so try it first and modify accordingly before running it in the real folder.



                            EDIT: Refer to shlomif's answer if you want the script to be more general. Regular expressions are useful for finding and matching patterns in strings.






                            share|improve this answer






























                              0














                              The easiest way to do this would be to locate the python script inside the folder the files are located. Assuming that all the filenames have the same amount of characters before the first number, then you'd need something like:



                              import os
                              for file in os.listdir('.'):
                              if '.py' not in file: #not the python script
                              os.rename(file, file[indexOfFirstNum:])


                              I haven't tested the script so try it first and modify accordingly before running it in the real folder.



                              EDIT: Refer to shlomif's answer if you want the script to be more general. Regular expressions are useful for finding and matching patterns in strings.






                              share|improve this answer




























                                0












                                0








                                0







                                The easiest way to do this would be to locate the python script inside the folder the files are located. Assuming that all the filenames have the same amount of characters before the first number, then you'd need something like:



                                import os
                                for file in os.listdir('.'):
                                if '.py' not in file: #not the python script
                                os.rename(file, file[indexOfFirstNum:])


                                I haven't tested the script so try it first and modify accordingly before running it in the real folder.



                                EDIT: Refer to shlomif's answer if you want the script to be more general. Regular expressions are useful for finding and matching patterns in strings.






                                share|improve this answer















                                The easiest way to do this would be to locate the python script inside the folder the files are located. Assuming that all the filenames have the same amount of characters before the first number, then you'd need something like:



                                import os
                                for file in os.listdir('.'):
                                if '.py' not in file: #not the python script
                                os.rename(file, file[indexOfFirstNum:])


                                I haven't tested the script so try it first and modify accordingly before running it in the real folder.



                                EDIT: Refer to shlomif's answer if you want the script to be more general. Regular expressions are useful for finding and matching patterns in strings.







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 21 '18 at 22:41

























                                answered Nov 21 '18 at 22:34









                                Johnny BeltranJohnny Beltran

                                719




                                719























                                    0














                                    Here is the Perl one liner solution:



                                    $ ls -l
                                    total 0
                                    -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
                                    -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
                                    -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
                                    -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
                                    -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
                                    -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV6.jpg

                                    $ perl -ne ' BEGIN { foreach $f (glob("image-w*.jpg")) { $newf=$f, $newf=~s/(.[^d]*)-(d+)(.*)/23/g; rename $f, "$newf" } exit } '

                                    $ ls -l
                                    total 0
                                    -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV1.jpg
                                    -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV2.jpg
                                    -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV3.jpg
                                    -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV4.jpg
                                    -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV5.jpg
                                    -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV6.jpg

                                    $





                                    share|improve this answer




























                                      0














                                      Here is the Perl one liner solution:



                                      $ ls -l
                                      total 0
                                      -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
                                      -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
                                      -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
                                      -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
                                      -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
                                      -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV6.jpg

                                      $ perl -ne ' BEGIN { foreach $f (glob("image-w*.jpg")) { $newf=$f, $newf=~s/(.[^d]*)-(d+)(.*)/23/g; rename $f, "$newf" } exit } '

                                      $ ls -l
                                      total 0
                                      -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV1.jpg
                                      -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV2.jpg
                                      -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV3.jpg
                                      -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV4.jpg
                                      -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV5.jpg
                                      -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV6.jpg

                                      $





                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        Here is the Perl one liner solution:



                                        $ ls -l
                                        total 0
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV6.jpg

                                        $ perl -ne ' BEGIN { foreach $f (glob("image-w*.jpg")) { $newf=$f, $newf=~s/(.[^d]*)-(d+)(.*)/23/g; rename $f, "$newf" } exit } '

                                        $ ls -l
                                        total 0
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV1.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV2.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV3.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV4.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV5.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV6.jpg

                                        $





                                        share|improve this answer













                                        Here is the Perl one liner solution:



                                        $ ls -l
                                        total 0
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV6.jpg

                                        $ perl -ne ' BEGIN { foreach $f (glob("image-w*.jpg")) { $newf=$f, $newf=~s/(.[^d]*)-(d+)(.*)/23/g; rename $f, "$newf" } exit } '

                                        $ ls -l
                                        total 0
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV1.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV2.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV3.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV4.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV5.jpg
                                        -rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV6.jpg

                                        $






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 22 '18 at 3:01









                                        stack0114106stack0114106

                                        4,4002422




                                        4,4002422























                                            0














                                            You most likely can do this with rename if your distro provides this command. For example:



                                            $ rename 'image-w-inch-bob-bob-bob-bob-' '' image-w-"


                                            or with PCRE Regex as the expression:



                                            $ rename 's/image-w-inch-bob-bob-bob-bob-//' image-w-*


                                            rename can batch rename a set of files according to a pattern match, including by regular expression. Note that the are several versions of rename which accepts different syntax, you should consult your local man and/or the command's help page to determine which version you are using and the precise syntax to use.






                                            share|improve this answer






























                                              0














                                              You most likely can do this with rename if your distro provides this command. For example:



                                              $ rename 'image-w-inch-bob-bob-bob-bob-' '' image-w-"


                                              or with PCRE Regex as the expression:



                                              $ rename 's/image-w-inch-bob-bob-bob-bob-//' image-w-*


                                              rename can batch rename a set of files according to a pattern match, including by regular expression. Note that the are several versions of rename which accepts different syntax, you should consult your local man and/or the command's help page to determine which version you are using and the precise syntax to use.






                                              share|improve this answer




























                                                0












                                                0








                                                0







                                                You most likely can do this with rename if your distro provides this command. For example:



                                                $ rename 'image-w-inch-bob-bob-bob-bob-' '' image-w-"


                                                or with PCRE Regex as the expression:



                                                $ rename 's/image-w-inch-bob-bob-bob-bob-//' image-w-*


                                                rename can batch rename a set of files according to a pattern match, including by regular expression. Note that the are several versions of rename which accepts different syntax, you should consult your local man and/or the command's help page to determine which version you are using and the precise syntax to use.






                                                share|improve this answer















                                                You most likely can do this with rename if your distro provides this command. For example:



                                                $ rename 'image-w-inch-bob-bob-bob-bob-' '' image-w-"


                                                or with PCRE Regex as the expression:



                                                $ rename 's/image-w-inch-bob-bob-bob-bob-//' image-w-*


                                                rename can batch rename a set of files according to a pattern match, including by regular expression. Note that the are several versions of rename which accepts different syntax, you should consult your local man and/or the command's help page to determine which version you are using and the precise syntax to use.







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Nov 22 '18 at 12:20

























                                                answered Nov 21 '18 at 22:19









                                                Lie RyanLie Ryan

                                                45.5k970123




                                                45.5k970123






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421214%2fscript-to-remove-string-up-to-first-number%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    這個網誌中的熱門文章

                                                    Academy of Television Arts & Sciences

                                                    L'Équipe

                                                    1995 France bombings