How to append records in a loop?

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I want to append the output of the below loop one below the other, but in output file i get only the result of 'abc3'



Find the script below, Please let me know the change, thanks in advance.



for i in abc1 abc2 abc3
do
SELECT_QUERY_1="select substring('$i',4,len('$i')-7) as Table_Name,COLUMN_NAME,ORDINAL_POSITION,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = '$i' and ORDINAL_POSITION <= (select max(ORDINAL_POSITION)-4 from INFORMATION_SCHEMA.COLUMNS where table_name = '$i')"

echo -e `/opt/mssql-tools/bin/sqlcmd -S server_name -U username -P user -d db -Q "$SELECT_QUERY_1" -o "out.txt"`
sed "s/^[ t]*//" -i out.txt

mv out.txt /home/results/out.txt

done









share|improve this question






























    0















    I want to append the output of the below loop one below the other, but in output file i get only the result of 'abc3'



    Find the script below, Please let me know the change, thanks in advance.



    for i in abc1 abc2 abc3
    do
    SELECT_QUERY_1="select substring('$i',4,len('$i')-7) as Table_Name,COLUMN_NAME,ORDINAL_POSITION,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH from INFORMATION_SCHEMA.COLUMNS
    where TABLE_NAME = '$i' and ORDINAL_POSITION <= (select max(ORDINAL_POSITION)-4 from INFORMATION_SCHEMA.COLUMNS where table_name = '$i')"

    echo -e `/opt/mssql-tools/bin/sqlcmd -S server_name -U username -P user -d db -Q "$SELECT_QUERY_1" -o "out.txt"`
    sed "s/^[ t]*//" -i out.txt

    mv out.txt /home/results/out.txt

    done









    share|improve this question


























      0












      0








      0








      I want to append the output of the below loop one below the other, but in output file i get only the result of 'abc3'



      Find the script below, Please let me know the change, thanks in advance.



      for i in abc1 abc2 abc3
      do
      SELECT_QUERY_1="select substring('$i',4,len('$i')-7) as Table_Name,COLUMN_NAME,ORDINAL_POSITION,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH from INFORMATION_SCHEMA.COLUMNS
      where TABLE_NAME = '$i' and ORDINAL_POSITION <= (select max(ORDINAL_POSITION)-4 from INFORMATION_SCHEMA.COLUMNS where table_name = '$i')"

      echo -e `/opt/mssql-tools/bin/sqlcmd -S server_name -U username -P user -d db -Q "$SELECT_QUERY_1" -o "out.txt"`
      sed "s/^[ t]*//" -i out.txt

      mv out.txt /home/results/out.txt

      done









      share|improve this question
















      I want to append the output of the below loop one below the other, but in output file i get only the result of 'abc3'



      Find the script below, Please let me know the change, thanks in advance.



      for i in abc1 abc2 abc3
      do
      SELECT_QUERY_1="select substring('$i',4,len('$i')-7) as Table_Name,COLUMN_NAME,ORDINAL_POSITION,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH from INFORMATION_SCHEMA.COLUMNS
      where TABLE_NAME = '$i' and ORDINAL_POSITION <= (select max(ORDINAL_POSITION)-4 from INFORMATION_SCHEMA.COLUMNS where table_name = '$i')"

      echo -e `/opt/mssql-tools/bin/sqlcmd -S server_name -U username -P user -d db -Q "$SELECT_QUERY_1" -o "out.txt"`
      sed "s/^[ t]*//" -i out.txt

      mv out.txt /home/results/out.txt

      done






      linux shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 12 at 12:48









      steeldriver

      37.7k45389




      37.7k45389










      asked Mar 12 at 11:55









      VenkatVenkat

      31




      31




















          2 Answers
          2






          active

          oldest

          votes


















          0














          change the line
          mv out.txt /home/results/out.txt



          to



          cat out.txt >> /home/results/out.txt



          to get all the three results in one file. Because with the mv command, the file is overwritten every time. That`s why you only get the last result.



          If you do not want to keep the old data in that file, just need to



          rm /home/results/out.txt



          at the begining of the script






          share|improve this answer

























          • Thank you, but every time i run the script the file must be overwritten, I dont want the old data in there.

            – Venkat
            Mar 12 at 13:27











          • @Venkat In that case you just need to rm /home/results/result.txt at the begining of the script

            – Juan
            Mar 12 at 13:28







          • 1





            Thank you @juan for your response.

            – Venkat
            Mar 12 at 15:17











          • In the above same query, I am getting trailing white space for all the column outputs. I dont want any trailing space for any columns. How do I control this?

            – Venkat
            Mar 14 at 3:24












          • @Venkat, I think you should start a new question. Because is a different topic and this one is very related to sqlcmd. Possibly with some arguments would be fixed. Nevertheless, the answer for the original question was a GNU/Linux console commands solution only.

            – Juan
            Mar 14 at 13:31


















          0














          echo -e `/opt/mssql-tools/bin/sqlcmd ... -o "out.txt"`


          Here, you're using command substitution to catch the output of sqlcmd, and then printing it again with echo. This is seldom useful, you could probably just run /opt/mssql-tools/bin/sqlcmd ... -o "out.txt" without the command substitution or echo. (There's is a small difference though, unquoted command substitution+echo transforms all whitespace to single spaces.)



          Now, assuming what you want is to concatenate all the outputs of all invocations of sqlcmd to a single file, you could use an appending output (>>) to direct the result directly to the final file.



          If sqlcmd outputs normally to stdout, you could just do:



          /opt/mssql-tools/bin/sqlcmd -S server_name -U username -P user 
          -d db -Q "$SELECT_QUERY_1" | sed "s/^[ t]*//" >> /home/results/out.txt


          If it doesn't support that, you can use /dev/stdout as the output file:



          /opt/mssql-tools/bin/sqlcmd -S server_name -U username -P user 
          -d db -Q "$SELECT_QUERY_1" -o /dev/stdout | sed "s/^[ t]*//" >> /home/results/out.txt





          share|improve this answer























            Your Answer








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

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

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            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%2f505855%2fhow-to-append-records-in-a-loop%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














            change the line
            mv out.txt /home/results/out.txt



            to



            cat out.txt >> /home/results/out.txt



            to get all the three results in one file. Because with the mv command, the file is overwritten every time. That`s why you only get the last result.



            If you do not want to keep the old data in that file, just need to



            rm /home/results/out.txt



            at the begining of the script






            share|improve this answer

























            • Thank you, but every time i run the script the file must be overwritten, I dont want the old data in there.

              – Venkat
              Mar 12 at 13:27











            • @Venkat In that case you just need to rm /home/results/result.txt at the begining of the script

              – Juan
              Mar 12 at 13:28







            • 1





              Thank you @juan for your response.

              – Venkat
              Mar 12 at 15:17











            • In the above same query, I am getting trailing white space for all the column outputs. I dont want any trailing space for any columns. How do I control this?

              – Venkat
              Mar 14 at 3:24












            • @Venkat, I think you should start a new question. Because is a different topic and this one is very related to sqlcmd. Possibly with some arguments would be fixed. Nevertheless, the answer for the original question was a GNU/Linux console commands solution only.

              – Juan
              Mar 14 at 13:31















            0














            change the line
            mv out.txt /home/results/out.txt



            to



            cat out.txt >> /home/results/out.txt



            to get all the three results in one file. Because with the mv command, the file is overwritten every time. That`s why you only get the last result.



            If you do not want to keep the old data in that file, just need to



            rm /home/results/out.txt



            at the begining of the script






            share|improve this answer

























            • Thank you, but every time i run the script the file must be overwritten, I dont want the old data in there.

              – Venkat
              Mar 12 at 13:27











            • @Venkat In that case you just need to rm /home/results/result.txt at the begining of the script

              – Juan
              Mar 12 at 13:28







            • 1





              Thank you @juan for your response.

              – Venkat
              Mar 12 at 15:17











            • In the above same query, I am getting trailing white space for all the column outputs. I dont want any trailing space for any columns. How do I control this?

              – Venkat
              Mar 14 at 3:24












            • @Venkat, I think you should start a new question. Because is a different topic and this one is very related to sqlcmd. Possibly with some arguments would be fixed. Nevertheless, the answer for the original question was a GNU/Linux console commands solution only.

              – Juan
              Mar 14 at 13:31













            0












            0








            0







            change the line
            mv out.txt /home/results/out.txt



            to



            cat out.txt >> /home/results/out.txt



            to get all the three results in one file. Because with the mv command, the file is overwritten every time. That`s why you only get the last result.



            If you do not want to keep the old data in that file, just need to



            rm /home/results/out.txt



            at the begining of the script






            share|improve this answer















            change the line
            mv out.txt /home/results/out.txt



            to



            cat out.txt >> /home/results/out.txt



            to get all the three results in one file. Because with the mv command, the file is overwritten every time. That`s why you only get the last result.



            If you do not want to keep the old data in that file, just need to



            rm /home/results/out.txt



            at the begining of the script







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 12 at 13:56

























            answered Mar 12 at 13:05









            JuanJuan

            201210




            201210












            • Thank you, but every time i run the script the file must be overwritten, I dont want the old data in there.

              – Venkat
              Mar 12 at 13:27











            • @Venkat In that case you just need to rm /home/results/result.txt at the begining of the script

              – Juan
              Mar 12 at 13:28







            • 1





              Thank you @juan for your response.

              – Venkat
              Mar 12 at 15:17











            • In the above same query, I am getting trailing white space for all the column outputs. I dont want any trailing space for any columns. How do I control this?

              – Venkat
              Mar 14 at 3:24












            • @Venkat, I think you should start a new question. Because is a different topic and this one is very related to sqlcmd. Possibly with some arguments would be fixed. Nevertheless, the answer for the original question was a GNU/Linux console commands solution only.

              – Juan
              Mar 14 at 13:31

















            • Thank you, but every time i run the script the file must be overwritten, I dont want the old data in there.

              – Venkat
              Mar 12 at 13:27











            • @Venkat In that case you just need to rm /home/results/result.txt at the begining of the script

              – Juan
              Mar 12 at 13:28







            • 1





              Thank you @juan for your response.

              – Venkat
              Mar 12 at 15:17











            • In the above same query, I am getting trailing white space for all the column outputs. I dont want any trailing space for any columns. How do I control this?

              – Venkat
              Mar 14 at 3:24












            • @Venkat, I think you should start a new question. Because is a different topic and this one is very related to sqlcmd. Possibly with some arguments would be fixed. Nevertheless, the answer for the original question was a GNU/Linux console commands solution only.

              – Juan
              Mar 14 at 13:31
















            Thank you, but every time i run the script the file must be overwritten, I dont want the old data in there.

            – Venkat
            Mar 12 at 13:27





            Thank you, but every time i run the script the file must be overwritten, I dont want the old data in there.

            – Venkat
            Mar 12 at 13:27













            @Venkat In that case you just need to rm /home/results/result.txt at the begining of the script

            – Juan
            Mar 12 at 13:28






            @Venkat In that case you just need to rm /home/results/result.txt at the begining of the script

            – Juan
            Mar 12 at 13:28





            1




            1





            Thank you @juan for your response.

            – Venkat
            Mar 12 at 15:17





            Thank you @juan for your response.

            – Venkat
            Mar 12 at 15:17













            In the above same query, I am getting trailing white space for all the column outputs. I dont want any trailing space for any columns. How do I control this?

            – Venkat
            Mar 14 at 3:24






            In the above same query, I am getting trailing white space for all the column outputs. I dont want any trailing space for any columns. How do I control this?

            – Venkat
            Mar 14 at 3:24














            @Venkat, I think you should start a new question. Because is a different topic and this one is very related to sqlcmd. Possibly with some arguments would be fixed. Nevertheless, the answer for the original question was a GNU/Linux console commands solution only.

            – Juan
            Mar 14 at 13:31





            @Venkat, I think you should start a new question. Because is a different topic and this one is very related to sqlcmd. Possibly with some arguments would be fixed. Nevertheless, the answer for the original question was a GNU/Linux console commands solution only.

            – Juan
            Mar 14 at 13:31













            0














            echo -e `/opt/mssql-tools/bin/sqlcmd ... -o "out.txt"`


            Here, you're using command substitution to catch the output of sqlcmd, and then printing it again with echo. This is seldom useful, you could probably just run /opt/mssql-tools/bin/sqlcmd ... -o "out.txt" without the command substitution or echo. (There's is a small difference though, unquoted command substitution+echo transforms all whitespace to single spaces.)



            Now, assuming what you want is to concatenate all the outputs of all invocations of sqlcmd to a single file, you could use an appending output (>>) to direct the result directly to the final file.



            If sqlcmd outputs normally to stdout, you could just do:



            /opt/mssql-tools/bin/sqlcmd -S server_name -U username -P user 
            -d db -Q "$SELECT_QUERY_1" | sed "s/^[ t]*//" >> /home/results/out.txt


            If it doesn't support that, you can use /dev/stdout as the output file:



            /opt/mssql-tools/bin/sqlcmd -S server_name -U username -P user 
            -d db -Q "$SELECT_QUERY_1" -o /dev/stdout | sed "s/^[ t]*//" >> /home/results/out.txt





            share|improve this answer



























              0














              echo -e `/opt/mssql-tools/bin/sqlcmd ... -o "out.txt"`


              Here, you're using command substitution to catch the output of sqlcmd, and then printing it again with echo. This is seldom useful, you could probably just run /opt/mssql-tools/bin/sqlcmd ... -o "out.txt" without the command substitution or echo. (There's is a small difference though, unquoted command substitution+echo transforms all whitespace to single spaces.)



              Now, assuming what you want is to concatenate all the outputs of all invocations of sqlcmd to a single file, you could use an appending output (>>) to direct the result directly to the final file.



              If sqlcmd outputs normally to stdout, you could just do:



              /opt/mssql-tools/bin/sqlcmd -S server_name -U username -P user 
              -d db -Q "$SELECT_QUERY_1" | sed "s/^[ t]*//" >> /home/results/out.txt


              If it doesn't support that, you can use /dev/stdout as the output file:



              /opt/mssql-tools/bin/sqlcmd -S server_name -U username -P user 
              -d db -Q "$SELECT_QUERY_1" -o /dev/stdout | sed "s/^[ t]*//" >> /home/results/out.txt





              share|improve this answer

























                0












                0








                0







                echo -e `/opt/mssql-tools/bin/sqlcmd ... -o "out.txt"`


                Here, you're using command substitution to catch the output of sqlcmd, and then printing it again with echo. This is seldom useful, you could probably just run /opt/mssql-tools/bin/sqlcmd ... -o "out.txt" without the command substitution or echo. (There's is a small difference though, unquoted command substitution+echo transforms all whitespace to single spaces.)



                Now, assuming what you want is to concatenate all the outputs of all invocations of sqlcmd to a single file, you could use an appending output (>>) to direct the result directly to the final file.



                If sqlcmd outputs normally to stdout, you could just do:



                /opt/mssql-tools/bin/sqlcmd -S server_name -U username -P user 
                -d db -Q "$SELECT_QUERY_1" | sed "s/^[ t]*//" >> /home/results/out.txt


                If it doesn't support that, you can use /dev/stdout as the output file:



                /opt/mssql-tools/bin/sqlcmd -S server_name -U username -P user 
                -d db -Q "$SELECT_QUERY_1" -o /dev/stdout | sed "s/^[ t]*//" >> /home/results/out.txt





                share|improve this answer













                echo -e `/opt/mssql-tools/bin/sqlcmd ... -o "out.txt"`


                Here, you're using command substitution to catch the output of sqlcmd, and then printing it again with echo. This is seldom useful, you could probably just run /opt/mssql-tools/bin/sqlcmd ... -o "out.txt" without the command substitution or echo. (There's is a small difference though, unquoted command substitution+echo transforms all whitespace to single spaces.)



                Now, assuming what you want is to concatenate all the outputs of all invocations of sqlcmd to a single file, you could use an appending output (>>) to direct the result directly to the final file.



                If sqlcmd outputs normally to stdout, you could just do:



                /opt/mssql-tools/bin/sqlcmd -S server_name -U username -P user 
                -d db -Q "$SELECT_QUERY_1" | sed "s/^[ t]*//" >> /home/results/out.txt


                If it doesn't support that, you can use /dev/stdout as the output file:



                /opt/mssql-tools/bin/sqlcmd -S server_name -U username -P user 
                -d db -Q "$SELECT_QUERY_1" -o /dev/stdout | sed "s/^[ t]*//" >> /home/results/out.txt






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 12 at 13:23









                ilkkachuilkkachu

                63.3k10104181




                63.3k10104181



























                    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%2f505855%2fhow-to-append-records-in-a-loop%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 check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay