Looping over files with specific matching numbers2019 Community Moderator Electioninit.d script is not starting under virtualboxIncrement Numbers in URL?Rename files in bash scriptBash - Regex to match tuples of numbersHow to make bash glob a string variable?Bash - Replacing random number between quotesAddition of extremely large numbers in shell scriptFind filename inside tar archive in different directoryCommand to extract value between two variables and store it in a variableMaintaining several constants for several bash scripts

How does electrical safety system work on ISS?

Creating two special characters

Is this part of the description of the Archfey warlock's Misty Escape feature redundant?

Make a Bowl of Alphabet Soup

awk assign to multiple variables at once

What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?

Is there a nicer/politer/more positive alternative for "negates"?

Why is it that I can sometimes guess the next note?

Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?

Quoting Keynes in a lecture

Strong empirical falsification of quantum mechanics based on vacuum energy density?

Does Doodling or Improvising on the Piano Have Any Benefits?

How to explain what's wrong with this application of the chain rule?

Did the UK lift the requirement for registering SIM cards?

Mimic lecturing on blackboard, facing audience

Why is the "ls" command showing permissions of files in a FAT32 partition?

How to get directions in deep space?

Why is the Sun approximated as a black body at ~ 5800 K?

What's the name of the logical fallacy where a debater extends a statement far beyond the original statement to make it true?

Has any country ever had 2 former presidents in jail simultaneously?

What does "Scientists rise up against statistical significance" mean? (Comment in Nature)

What (the heck) is a Super Worm Equinox Moon?

Is there any evidence that Cleopatra and Caesarion considered fleeing to India to escape the Romans?

Which Article Helped Get Rid of Technobabble in RPGs?



Looping over files with specific matching numbers



2019 Community Moderator Electioninit.d script is not starting under virtualboxIncrement Numbers in URL?Rename files in bash scriptBash - Regex to match tuples of numbersHow to make bash glob a string variable?Bash - Replacing random number between quotesAddition of extremely large numbers in shell scriptFind filename inside tar archive in different directoryCommand to extract value between two variables and store it in a variableMaintaining several constants for several bash scripts










1















I'm using a Bash script, and I have two input numbers. For simplicity, let's say the first variable is called Start and the second variable is Stop, and each can be a number between 1000 and 2000.



I have files that are arranged via names that match the starting two digits of each number. For example, one file is called:



/path/to/files11, which is a file corresponding to any number between (and including) 1100 to 1199



Now, given Stop and Start, I want to use a loop that sets the proper files to a string in the following manner: If Start = 1923 and Stop = 2267, then I want to do the following (pseudocode):



MyVariable = "/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22"



but I want to be able to do this for any number in my range.



Any advice on how to select the proper starting and stopping files based on the first two digits and looping over the numbers to get the right string?










share|improve this question









New contributor




theta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    1















    I'm using a Bash script, and I have two input numbers. For simplicity, let's say the first variable is called Start and the second variable is Stop, and each can be a number between 1000 and 2000.



    I have files that are arranged via names that match the starting two digits of each number. For example, one file is called:



    /path/to/files11, which is a file corresponding to any number between (and including) 1100 to 1199



    Now, given Stop and Start, I want to use a loop that sets the proper files to a string in the following manner: If Start = 1923 and Stop = 2267, then I want to do the following (pseudocode):



    MyVariable = "/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22"



    but I want to be able to do this for any number in my range.



    Any advice on how to select the proper starting and stopping files based on the first two digits and looping over the numbers to get the right string?










    share|improve this question









    New contributor




    theta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      1












      1








      1








      I'm using a Bash script, and I have two input numbers. For simplicity, let's say the first variable is called Start and the second variable is Stop, and each can be a number between 1000 and 2000.



      I have files that are arranged via names that match the starting two digits of each number. For example, one file is called:



      /path/to/files11, which is a file corresponding to any number between (and including) 1100 to 1199



      Now, given Stop and Start, I want to use a loop that sets the proper files to a string in the following manner: If Start = 1923 and Stop = 2267, then I want to do the following (pseudocode):



      MyVariable = "/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22"



      but I want to be able to do this for any number in my range.



      Any advice on how to select the proper starting and stopping files based on the first two digits and looping over the numbers to get the right string?










      share|improve this question









      New contributor




      theta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I'm using a Bash script, and I have two input numbers. For simplicity, let's say the first variable is called Start and the second variable is Stop, and each can be a number between 1000 and 2000.



      I have files that are arranged via names that match the starting two digits of each number. For example, one file is called:



      /path/to/files11, which is a file corresponding to any number between (and including) 1100 to 1199



      Now, given Stop and Start, I want to use a loop that sets the proper files to a string in the following manner: If Start = 1923 and Stop = 2267, then I want to do the following (pseudocode):



      MyVariable = "/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22"



      but I want to be able to do this for any number in my range.



      Any advice on how to select the proper starting and stopping files based on the first two digits and looping over the numbers to get the right string?







      bash shell-script






      share|improve this question









      New contributor




      theta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      theta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited yesterday









      Rui F Ribeiro

      41.6k1483141




      41.6k1483141






      New contributor




      theta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked yesterday









      thetatheta

      82




      82




      New contributor




      theta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      theta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      theta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes


















          0














          Using bash



          Try:



          Start=1923
          Stop=2267
          MyVar=
          for ((i=Start/100; i<=Stop/100; i++)); do
          MyVar="$MyVar;/path/to/files$i"
          done
          MyVar=$MyVar:1
          echo "$MyVar"


          The above echo statement produces:



          /path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22


          How it works:




          • MyVar=



            This makes sure that the variable MyVar is empty.




          • for ((i=Start/100; i<=Stop/100; i++)); do



            This starts a loop over variable i.




          • MyVar="$MyVar;/path/to/files$i"



            This appends a string to MyVar every time the loop is run.




          • done



            This signals the end of the loop.




          • MyVar=$MyVar:1



            This removes the unwanted semicolon from the beginning of the string.




          • echo "$MyVar"



            This displays the result.



          Using awk



          Using very similar logic:



          MyVar=$(awk -v Start=1923 -v Stop=2267 'BEGINfor (i=int(Start/100);i<=int(Stop/100);i++) MyVar=MyVar";/path/to/files" i; print substr(MyVar,2)')
          echo "$MyVar"


          The above echo statement produces:



          /path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22





          share|improve this answer




















          • 1





            Thank you so much!!

            – theta
            yesterday


















          0














          This looks like a job for Brace Expansion, plus a bit of eval over string manipulation:



          MyVariable=$(eval "printf '%s' /path/to/files$Start:0:2..$Stop:0:2;")


          You then may want to get rid of the final semi-colon via $MyVariable%;



          In the above example I'm assuming that your $Start and $Stop truly are always at least a 3 digits number, else you will need to adjust the string manipulations.






          share|improve this answer










          New contributor




          LL3 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.



















            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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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
            );



            );






            theta is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507577%2flooping-over-files-with-specific-matching-numbers%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Using bash



            Try:



            Start=1923
            Stop=2267
            MyVar=
            for ((i=Start/100; i<=Stop/100; i++)); do
            MyVar="$MyVar;/path/to/files$i"
            done
            MyVar=$MyVar:1
            echo "$MyVar"


            The above echo statement produces:



            /path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22


            How it works:




            • MyVar=



              This makes sure that the variable MyVar is empty.




            • for ((i=Start/100; i<=Stop/100; i++)); do



              This starts a loop over variable i.




            • MyVar="$MyVar;/path/to/files$i"



              This appends a string to MyVar every time the loop is run.




            • done



              This signals the end of the loop.




            • MyVar=$MyVar:1



              This removes the unwanted semicolon from the beginning of the string.




            • echo "$MyVar"



              This displays the result.



            Using awk



            Using very similar logic:



            MyVar=$(awk -v Start=1923 -v Stop=2267 'BEGINfor (i=int(Start/100);i<=int(Stop/100);i++) MyVar=MyVar";/path/to/files" i; print substr(MyVar,2)')
            echo "$MyVar"


            The above echo statement produces:



            /path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22





            share|improve this answer




















            • 1





              Thank you so much!!

              – theta
              yesterday















            0














            Using bash



            Try:



            Start=1923
            Stop=2267
            MyVar=
            for ((i=Start/100; i<=Stop/100; i++)); do
            MyVar="$MyVar;/path/to/files$i"
            done
            MyVar=$MyVar:1
            echo "$MyVar"


            The above echo statement produces:



            /path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22


            How it works:




            • MyVar=



              This makes sure that the variable MyVar is empty.




            • for ((i=Start/100; i<=Stop/100; i++)); do



              This starts a loop over variable i.




            • MyVar="$MyVar;/path/to/files$i"



              This appends a string to MyVar every time the loop is run.




            • done



              This signals the end of the loop.




            • MyVar=$MyVar:1



              This removes the unwanted semicolon from the beginning of the string.




            • echo "$MyVar"



              This displays the result.



            Using awk



            Using very similar logic:



            MyVar=$(awk -v Start=1923 -v Stop=2267 'BEGINfor (i=int(Start/100);i<=int(Stop/100);i++) MyVar=MyVar";/path/to/files" i; print substr(MyVar,2)')
            echo "$MyVar"


            The above echo statement produces:



            /path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22





            share|improve this answer




















            • 1





              Thank you so much!!

              – theta
              yesterday













            0












            0








            0







            Using bash



            Try:



            Start=1923
            Stop=2267
            MyVar=
            for ((i=Start/100; i<=Stop/100; i++)); do
            MyVar="$MyVar;/path/to/files$i"
            done
            MyVar=$MyVar:1
            echo "$MyVar"


            The above echo statement produces:



            /path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22


            How it works:




            • MyVar=



              This makes sure that the variable MyVar is empty.




            • for ((i=Start/100; i<=Stop/100; i++)); do



              This starts a loop over variable i.




            • MyVar="$MyVar;/path/to/files$i"



              This appends a string to MyVar every time the loop is run.




            • done



              This signals the end of the loop.




            • MyVar=$MyVar:1



              This removes the unwanted semicolon from the beginning of the string.




            • echo "$MyVar"



              This displays the result.



            Using awk



            Using very similar logic:



            MyVar=$(awk -v Start=1923 -v Stop=2267 'BEGINfor (i=int(Start/100);i<=int(Stop/100);i++) MyVar=MyVar";/path/to/files" i; print substr(MyVar,2)')
            echo "$MyVar"


            The above echo statement produces:



            /path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22





            share|improve this answer















            Using bash



            Try:



            Start=1923
            Stop=2267
            MyVar=
            for ((i=Start/100; i<=Stop/100; i++)); do
            MyVar="$MyVar;/path/to/files$i"
            done
            MyVar=$MyVar:1
            echo "$MyVar"


            The above echo statement produces:



            /path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22


            How it works:




            • MyVar=



              This makes sure that the variable MyVar is empty.




            • for ((i=Start/100; i<=Stop/100; i++)); do



              This starts a loop over variable i.




            • MyVar="$MyVar;/path/to/files$i"



              This appends a string to MyVar every time the loop is run.




            • done



              This signals the end of the loop.




            • MyVar=$MyVar:1



              This removes the unwanted semicolon from the beginning of the string.




            • echo "$MyVar"



              This displays the result.



            Using awk



            Using very similar logic:



            MyVar=$(awk -v Start=1923 -v Stop=2267 'BEGINfor (i=int(Start/100);i<=int(Stop/100);i++) MyVar=MyVar";/path/to/files" i; print substr(MyVar,2)')
            echo "$MyVar"


            The above echo statement produces:



            /path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited yesterday

























            answered yesterday









            John1024John1024

            47.7k5112126




            47.7k5112126







            • 1





              Thank you so much!!

              – theta
              yesterday












            • 1





              Thank you so much!!

              – theta
              yesterday







            1




            1





            Thank you so much!!

            – theta
            yesterday





            Thank you so much!!

            – theta
            yesterday













            0














            This looks like a job for Brace Expansion, plus a bit of eval over string manipulation:



            MyVariable=$(eval "printf '%s' /path/to/files$Start:0:2..$Stop:0:2;")


            You then may want to get rid of the final semi-colon via $MyVariable%;



            In the above example I'm assuming that your $Start and $Stop truly are always at least a 3 digits number, else you will need to adjust the string manipulations.






            share|improve this answer










            New contributor




            LL3 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.
























              0














              This looks like a job for Brace Expansion, plus a bit of eval over string manipulation:



              MyVariable=$(eval "printf '%s' /path/to/files$Start:0:2..$Stop:0:2;")


              You then may want to get rid of the final semi-colon via $MyVariable%;



              In the above example I'm assuming that your $Start and $Stop truly are always at least a 3 digits number, else you will need to adjust the string manipulations.






              share|improve this answer










              New contributor




              LL3 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






















                0












                0








                0







                This looks like a job for Brace Expansion, plus a bit of eval over string manipulation:



                MyVariable=$(eval "printf '%s' /path/to/files$Start:0:2..$Stop:0:2;")


                You then may want to get rid of the final semi-colon via $MyVariable%;



                In the above example I'm assuming that your $Start and $Stop truly are always at least a 3 digits number, else you will need to adjust the string manipulations.






                share|improve this answer










                New contributor




                LL3 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.










                This looks like a job for Brace Expansion, plus a bit of eval over string manipulation:



                MyVariable=$(eval "printf '%s' /path/to/files$Start:0:2..$Stop:0:2;")


                You then may want to get rid of the final semi-colon via $MyVariable%;



                In the above example I'm assuming that your $Start and $Stop truly are always at least a 3 digits number, else you will need to adjust the string manipulations.







                share|improve this answer










                New contributor




                LL3 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer








                edited yesterday





















                New contributor




                LL3 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered yesterday









                LL3LL3

                863




                863




                New contributor




                LL3 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                LL3 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                LL3 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.




















                    theta is a new contributor. Be nice, and check out our Code of Conduct.









                    draft saved

                    draft discarded


















                    theta is a new contributor. Be nice, and check out our Code of Conduct.












                    theta is a new contributor. Be nice, and check out our Code of Conduct.











                    theta is a new contributor. Be nice, and check out our Code of Conduct.














                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • 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%2funix.stackexchange.com%2fquestions%2f507577%2flooping-over-files-with-specific-matching-numbers%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







                    Popular posts from this blog

                    getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

                    Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

                    Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.