Running a loop from second line of a file in a bash script












1















I want to use the below file content as an input of for loop my script. As from second line we have id's which will be used to fetch content from db.



cat abc.txt 
id
27
27
27
27
23









share|improve this question

























  • Is the loop doing something with these numbers that could be done in a single awk script?

    – Kusalananda
    Mar 4 at 18:20











  • Related: Why is using a shell loop to process text considered bad practice?

    – Kusalananda
    Mar 4 at 18:45
















1















I want to use the below file content as an input of for loop my script. As from second line we have id's which will be used to fetch content from db.



cat abc.txt 
id
27
27
27
27
23









share|improve this question

























  • Is the loop doing something with these numbers that could be done in a single awk script?

    – Kusalananda
    Mar 4 at 18:20











  • Related: Why is using a shell loop to process text considered bad practice?

    – Kusalananda
    Mar 4 at 18:45














1












1








1








I want to use the below file content as an input of for loop my script. As from second line we have id's which will be used to fetch content from db.



cat abc.txt 
id
27
27
27
27
23









share|improve this question
















I want to use the below file content as an input of for loop my script. As from second line we have id's which will be used to fetch content from db.



cat abc.txt 
id
27
27
27
27
23






linux bash scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 28 at 8:55









Inian

5,1701429




5,1701429










asked Feb 28 at 8:47









user339272user339272

61




61













  • Is the loop doing something with these numbers that could be done in a single awk script?

    – Kusalananda
    Mar 4 at 18:20











  • Related: Why is using a shell loop to process text considered bad practice?

    – Kusalananda
    Mar 4 at 18:45



















  • Is the loop doing something with these numbers that could be done in a single awk script?

    – Kusalananda
    Mar 4 at 18:20











  • Related: Why is using a shell loop to process text considered bad practice?

    – Kusalananda
    Mar 4 at 18:45

















Is the loop doing something with these numbers that could be done in a single awk script?

– Kusalananda
Mar 4 at 18:20





Is the loop doing something with these numbers that could be done in a single awk script?

– Kusalananda
Mar 4 at 18:20













Related: Why is using a shell loop to process text considered bad practice?

– Kusalananda
Mar 4 at 18:45





Related: Why is using a shell loop to process text considered bad practice?

– Kusalananda
Mar 4 at 18:45










5 Answers
5






active

oldest

votes


















3














This can get as simple as the following:



{ read -r; while read -r line; do echo "$line"; done; } < abc.txt


The { here means to group the command. Using read -r we'll skip the first line!






share|improve this answer































    0














    You can filter it with sed before the proc.



    for example, instead of cat abc.txt you can sed 1d abc.txt to remove the first line.






    share|improve this answer































      0














      This works - even if the headline is not in the first row or there is an empty line:



      awk '!/^(id| *)$/{print$0}' file | while read line; do
      something $line
      done


      I used while in this example, because it is often preferable over for.



      That said - here is your for loop:



      for line in $(awk '!/^id$/{print$0}' file); do
      something $line
      done





      share|improve this answer































        -1














        Assuming the files containing your IDs is in the same directory as this script, this should work:



        #!/bin/bash
        content=$(tail -n +2 abc.txt)
        for line in $content; do
        echo "$line"
        done


        Output:



        27
        27
        27
        27
        23





        share|improve this answer
























        • I think for large file both answers will break.

          – Prvt_Yadv
          Feb 28 at 9:08






        • 2





          Don't read lines with for -- use a while read loop instead.

          – Gordon Davisson
          Feb 28 at 9:48



















        -1














        Not recommended if your file is large



        You can use like:



         for i in $(sed -e '1d' abc.txt)
        do
        something
        done





        share|improve this answer





















        • 1





          Don't read lines with for -- use a while read loop instead.

          – Gordon Davisson
          Feb 28 at 9:48











        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
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503509%2frunning-a-loop-from-second-line-of-a-file-in-a-bash-script%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









        3














        This can get as simple as the following:



        { read -r; while read -r line; do echo "$line"; done; } < abc.txt


        The { here means to group the command. Using read -r we'll skip the first line!






        share|improve this answer




























          3














          This can get as simple as the following:



          { read -r; while read -r line; do echo "$line"; done; } < abc.txt


          The { here means to group the command. Using read -r we'll skip the first line!






          share|improve this answer


























            3












            3








            3







            This can get as simple as the following:



            { read -r; while read -r line; do echo "$line"; done; } < abc.txt


            The { here means to group the command. Using read -r we'll skip the first line!






            share|improve this answer













            This can get as simple as the following:



            { read -r; while read -r line; do echo "$line"; done; } < abc.txt


            The { here means to group the command. Using read -r we'll skip the first line!







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 28 at 9:15









            Valentin BajramiValentin Bajrami

            6,13611728




            6,13611728

























                0














                You can filter it with sed before the proc.



                for example, instead of cat abc.txt you can sed 1d abc.txt to remove the first line.






                share|improve this answer




























                  0














                  You can filter it with sed before the proc.



                  for example, instead of cat abc.txt you can sed 1d abc.txt to remove the first line.






                  share|improve this answer


























                    0












                    0








                    0







                    You can filter it with sed before the proc.



                    for example, instead of cat abc.txt you can sed 1d abc.txt to remove the first line.






                    share|improve this answer













                    You can filter it with sed before the proc.



                    for example, instead of cat abc.txt you can sed 1d abc.txt to remove the first line.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 4 at 16:14









                    JuanJuan

                    1869




                    1869























                        0














                        This works - even if the headline is not in the first row or there is an empty line:



                        awk '!/^(id| *)$/{print$0}' file | while read line; do
                        something $line
                        done


                        I used while in this example, because it is often preferable over for.



                        That said - here is your for loop:



                        for line in $(awk '!/^id$/{print$0}' file); do
                        something $line
                        done





                        share|improve this answer




























                          0














                          This works - even if the headline is not in the first row or there is an empty line:



                          awk '!/^(id| *)$/{print$0}' file | while read line; do
                          something $line
                          done


                          I used while in this example, because it is often preferable over for.



                          That said - here is your for loop:



                          for line in $(awk '!/^id$/{print$0}' file); do
                          something $line
                          done





                          share|improve this answer


























                            0












                            0








                            0







                            This works - even if the headline is not in the first row or there is an empty line:



                            awk '!/^(id| *)$/{print$0}' file | while read line; do
                            something $line
                            done


                            I used while in this example, because it is often preferable over for.



                            That said - here is your for loop:



                            for line in $(awk '!/^id$/{print$0}' file); do
                            something $line
                            done





                            share|improve this answer













                            This works - even if the headline is not in the first row or there is an empty line:



                            awk '!/^(id| *)$/{print$0}' file | while read line; do
                            something $line
                            done


                            I used while in this example, because it is often preferable over for.



                            That said - here is your for loop:



                            for line in $(awk '!/^id$/{print$0}' file); do
                            something $line
                            done






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 4 at 17:52









                            sborskysborsky

                            811611




                            811611























                                -1














                                Assuming the files containing your IDs is in the same directory as this script, this should work:



                                #!/bin/bash
                                content=$(tail -n +2 abc.txt)
                                for line in $content; do
                                echo "$line"
                                done


                                Output:



                                27
                                27
                                27
                                27
                                23





                                share|improve this answer
























                                • I think for large file both answers will break.

                                  – Prvt_Yadv
                                  Feb 28 at 9:08






                                • 2





                                  Don't read lines with for -- use a while read loop instead.

                                  – Gordon Davisson
                                  Feb 28 at 9:48
















                                -1














                                Assuming the files containing your IDs is in the same directory as this script, this should work:



                                #!/bin/bash
                                content=$(tail -n +2 abc.txt)
                                for line in $content; do
                                echo "$line"
                                done


                                Output:



                                27
                                27
                                27
                                27
                                23





                                share|improve this answer
























                                • I think for large file both answers will break.

                                  – Prvt_Yadv
                                  Feb 28 at 9:08






                                • 2





                                  Don't read lines with for -- use a while read loop instead.

                                  – Gordon Davisson
                                  Feb 28 at 9:48














                                -1












                                -1








                                -1







                                Assuming the files containing your IDs is in the same directory as this script, this should work:



                                #!/bin/bash
                                content=$(tail -n +2 abc.txt)
                                for line in $content; do
                                echo "$line"
                                done


                                Output:



                                27
                                27
                                27
                                27
                                23





                                share|improve this answer













                                Assuming the files containing your IDs is in the same directory as this script, this should work:



                                #!/bin/bash
                                content=$(tail -n +2 abc.txt)
                                for line in $content; do
                                echo "$line"
                                done


                                Output:



                                27
                                27
                                27
                                27
                                23






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Feb 28 at 9:02









                                PankiPanki

                                778412




                                778412













                                • I think for large file both answers will break.

                                  – Prvt_Yadv
                                  Feb 28 at 9:08






                                • 2





                                  Don't read lines with for -- use a while read loop instead.

                                  – Gordon Davisson
                                  Feb 28 at 9:48



















                                • I think for large file both answers will break.

                                  – Prvt_Yadv
                                  Feb 28 at 9:08






                                • 2





                                  Don't read lines with for -- use a while read loop instead.

                                  – Gordon Davisson
                                  Feb 28 at 9:48

















                                I think for large file both answers will break.

                                – Prvt_Yadv
                                Feb 28 at 9:08





                                I think for large file both answers will break.

                                – Prvt_Yadv
                                Feb 28 at 9:08




                                2




                                2





                                Don't read lines with for -- use a while read loop instead.

                                – Gordon Davisson
                                Feb 28 at 9:48





                                Don't read lines with for -- use a while read loop instead.

                                – Gordon Davisson
                                Feb 28 at 9:48











                                -1














                                Not recommended if your file is large



                                You can use like:



                                 for i in $(sed -e '1d' abc.txt)
                                do
                                something
                                done





                                share|improve this answer





















                                • 1





                                  Don't read lines with for -- use a while read loop instead.

                                  – Gordon Davisson
                                  Feb 28 at 9:48
















                                -1














                                Not recommended if your file is large



                                You can use like:



                                 for i in $(sed -e '1d' abc.txt)
                                do
                                something
                                done





                                share|improve this answer





















                                • 1





                                  Don't read lines with for -- use a while read loop instead.

                                  – Gordon Davisson
                                  Feb 28 at 9:48














                                -1












                                -1








                                -1







                                Not recommended if your file is large



                                You can use like:



                                 for i in $(sed -e '1d' abc.txt)
                                do
                                something
                                done





                                share|improve this answer















                                Not recommended if your file is large



                                You can use like:



                                 for i in $(sed -e '1d' abc.txt)
                                do
                                something
                                done






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Feb 28 at 9:08

























                                answered Feb 28 at 9:03









                                Prvt_YadvPrvt_Yadv

                                2,88031227




                                2,88031227








                                • 1





                                  Don't read lines with for -- use a while read loop instead.

                                  – Gordon Davisson
                                  Feb 28 at 9:48














                                • 1





                                  Don't read lines with for -- use a while read loop instead.

                                  – Gordon Davisson
                                  Feb 28 at 9:48








                                1




                                1





                                Don't read lines with for -- use a while read loop instead.

                                – Gordon Davisson
                                Feb 28 at 9:48





                                Don't read lines with for -- use a while read loop instead.

                                – Gordon Davisson
                                Feb 28 at 9:48


















                                draft saved

                                draft discarded




















































                                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%2f503509%2frunning-a-loop-from-second-line-of-a-file-in-a-bash-script%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

                                How to make a Squid Proxy server?

                                Is this a new Fibonacci Identity?

                                19世紀