awk - Print all values with a suffix

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











up vote
0
down vote

favorite












I want to use awk to print all values in a delimited string with a static suffix appended to each one.



Input:



stack,over,flow


Delimiter:



Comma


Output:



stack suffix, over suffix, flow suffix









share|improve this question



























    up vote
    0
    down vote

    favorite












    I want to use awk to print all values in a delimited string with a static suffix appended to each one.



    Input:



    stack,over,flow


    Delimiter:



    Comma


    Output:



    stack suffix, over suffix, flow suffix









    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to use awk to print all values in a delimited string with a static suffix appended to each one.



      Input:



      stack,over,flow


      Delimiter:



      Comma


      Output:



      stack suffix, over suffix, flow suffix









      share|improve this question















      I want to use awk to print all values in a delimited string with a static suffix appended to each one.



      Input:



      stack,over,flow


      Delimiter:



      Comma


      Output:



      stack suffix, over suffix, flow suffix






      text-processing awk csv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 at 22:22









      Jeff Schaller

      37k1052121




      37k1052121










      asked Nov 26 at 21:24









      SQLadmin

      1095




      1095




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          awk -F ',' -v OFS=',' -vpre="prefix" -vsuf="suffix" 
          ' for (i=1;i<=NF;++i) $i = pre $i suf; print '


          For the input



          unix,and,linux


          this would produce



          prefixunixsuffix,prefixandsuffix,prefixlinuxsuffix


          The awk program modifies each field in a loop by adding a prefix and a suffix. The prefix and suffix strings are passed into the awk programs through assignments to the pre and suf variables on the command line.



          To add a space as prefix and the string suffix (with an initial space) as suffix, you would use



          awk -F ',' -v OFS=',' -vpre=" " -vsuf=" suffix" 
          ' for (i=1;i<=NF;++i) $i = pre $i suf; print '


          which produces



           unix suffix, and suffix, linux suffix





          share|improve this answer




















          • Hi @Kusalananda, its awesome, and one more doubt, If I don't want the comma, how can I remove that? like stack suffix over suffix flow suffix or replace with some other value
            – SQLadmin
            Nov 26 at 21:41











          • @SQLadmin Modify the output field separator (OFS) on the command line. For example, remove -v OFS completely to get space-separated output.
            – Kusalananda
            Nov 26 at 21:45

















          up vote
          2
          down vote













          Another Awk variant - making use of the internal output separators to avoid an explicit loop:



          awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'


          ex.



          echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'
          stack suffix, over suffix, flow suffix


          For space separated output you can append the default output field separator to the suffix



          $ echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s OFS; ORS= s RS $1=$1 1'
          stack suffix over suffix flow suffix





          share|improve this answer



























            up vote
            1
            down vote













            awk -F , 'print $1" suffix", $2" suffix", $3" suffix"' OFS=", " suffix


            awk -F , Sets the field separator as ,



            print $1" suffix", and so on and so forth prints a space and then suffix after the value



            OFS=", " Sets the output field separator as a comma with a space.



            Output:



             stack suffix, over suffix, flow suffix





            share|improve this answer




















            • Hey @Nasir, thanks, but in my case, there can be multiple values in input. So we need to use -F to get all values print with suffix.I don't know how to achieve this.
              – SQLadmin
              Nov 26 at 21:37










            • @SQLadmin The other answer works better. In the future, you need to specify that information in your question so that it can be accounted for.
              – Nasir Riley
              Nov 26 at 21:54










            Your Answer








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

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

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: 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%2f484303%2fawk-print-all-values-with-a-suffix%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            awk -F ',' -v OFS=',' -vpre="prefix" -vsuf="suffix" 
            ' for (i=1;i<=NF;++i) $i = pre $i suf; print '


            For the input



            unix,and,linux


            this would produce



            prefixunixsuffix,prefixandsuffix,prefixlinuxsuffix


            The awk program modifies each field in a loop by adding a prefix and a suffix. The prefix and suffix strings are passed into the awk programs through assignments to the pre and suf variables on the command line.



            To add a space as prefix and the string suffix (with an initial space) as suffix, you would use



            awk -F ',' -v OFS=',' -vpre=" " -vsuf=" suffix" 
            ' for (i=1;i<=NF;++i) $i = pre $i suf; print '


            which produces



             unix suffix, and suffix, linux suffix





            share|improve this answer




















            • Hi @Kusalananda, its awesome, and one more doubt, If I don't want the comma, how can I remove that? like stack suffix over suffix flow suffix or replace with some other value
              – SQLadmin
              Nov 26 at 21:41











            • @SQLadmin Modify the output field separator (OFS) on the command line. For example, remove -v OFS completely to get space-separated output.
              – Kusalananda
              Nov 26 at 21:45














            up vote
            2
            down vote



            accepted










            awk -F ',' -v OFS=',' -vpre="prefix" -vsuf="suffix" 
            ' for (i=1;i<=NF;++i) $i = pre $i suf; print '


            For the input



            unix,and,linux


            this would produce



            prefixunixsuffix,prefixandsuffix,prefixlinuxsuffix


            The awk program modifies each field in a loop by adding a prefix and a suffix. The prefix and suffix strings are passed into the awk programs through assignments to the pre and suf variables on the command line.



            To add a space as prefix and the string suffix (with an initial space) as suffix, you would use



            awk -F ',' -v OFS=',' -vpre=" " -vsuf=" suffix" 
            ' for (i=1;i<=NF;++i) $i = pre $i suf; print '


            which produces



             unix suffix, and suffix, linux suffix





            share|improve this answer




















            • Hi @Kusalananda, its awesome, and one more doubt, If I don't want the comma, how can I remove that? like stack suffix over suffix flow suffix or replace with some other value
              – SQLadmin
              Nov 26 at 21:41











            • @SQLadmin Modify the output field separator (OFS) on the command line. For example, remove -v OFS completely to get space-separated output.
              – Kusalananda
              Nov 26 at 21:45












            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            awk -F ',' -v OFS=',' -vpre="prefix" -vsuf="suffix" 
            ' for (i=1;i<=NF;++i) $i = pre $i suf; print '


            For the input



            unix,and,linux


            this would produce



            prefixunixsuffix,prefixandsuffix,prefixlinuxsuffix


            The awk program modifies each field in a loop by adding a prefix and a suffix. The prefix and suffix strings are passed into the awk programs through assignments to the pre and suf variables on the command line.



            To add a space as prefix and the string suffix (with an initial space) as suffix, you would use



            awk -F ',' -v OFS=',' -vpre=" " -vsuf=" suffix" 
            ' for (i=1;i<=NF;++i) $i = pre $i suf; print '


            which produces



             unix suffix, and suffix, linux suffix





            share|improve this answer












            awk -F ',' -v OFS=',' -vpre="prefix" -vsuf="suffix" 
            ' for (i=1;i<=NF;++i) $i = pre $i suf; print '


            For the input



            unix,and,linux


            this would produce



            prefixunixsuffix,prefixandsuffix,prefixlinuxsuffix


            The awk program modifies each field in a loop by adding a prefix and a suffix. The prefix and suffix strings are passed into the awk programs through assignments to the pre and suf variables on the command line.



            To add a space as prefix and the string suffix (with an initial space) as suffix, you would use



            awk -F ',' -v OFS=',' -vpre=" " -vsuf=" suffix" 
            ' for (i=1;i<=NF;++i) $i = pre $i suf; print '


            which produces



             unix suffix, and suffix, linux suffix






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 26 at 21:37









            Kusalananda

            118k16223364




            118k16223364











            • Hi @Kusalananda, its awesome, and one more doubt, If I don't want the comma, how can I remove that? like stack suffix over suffix flow suffix or replace with some other value
              – SQLadmin
              Nov 26 at 21:41











            • @SQLadmin Modify the output field separator (OFS) on the command line. For example, remove -v OFS completely to get space-separated output.
              – Kusalananda
              Nov 26 at 21:45
















            • Hi @Kusalananda, its awesome, and one more doubt, If I don't want the comma, how can I remove that? like stack suffix over suffix flow suffix or replace with some other value
              – SQLadmin
              Nov 26 at 21:41











            • @SQLadmin Modify the output field separator (OFS) on the command line. For example, remove -v OFS completely to get space-separated output.
              – Kusalananda
              Nov 26 at 21:45















            Hi @Kusalananda, its awesome, and one more doubt, If I don't want the comma, how can I remove that? like stack suffix over suffix flow suffix or replace with some other value
            – SQLadmin
            Nov 26 at 21:41





            Hi @Kusalananda, its awesome, and one more doubt, If I don't want the comma, how can I remove that? like stack suffix over suffix flow suffix or replace with some other value
            – SQLadmin
            Nov 26 at 21:41













            @SQLadmin Modify the output field separator (OFS) on the command line. For example, remove -v OFS completely to get space-separated output.
            – Kusalananda
            Nov 26 at 21:45




            @SQLadmin Modify the output field separator (OFS) on the command line. For example, remove -v OFS completely to get space-separated output.
            – Kusalananda
            Nov 26 at 21:45












            up vote
            2
            down vote













            Another Awk variant - making use of the internal output separators to avoid an explicit loop:



            awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'


            ex.



            echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'
            stack suffix, over suffix, flow suffix


            For space separated output you can append the default output field separator to the suffix



            $ echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s OFS; ORS= s RS $1=$1 1'
            stack suffix over suffix flow suffix





            share|improve this answer
























              up vote
              2
              down vote













              Another Awk variant - making use of the internal output separators to avoid an explicit loop:



              awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'


              ex.



              echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'
              stack suffix, over suffix, flow suffix


              For space separated output you can append the default output field separator to the suffix



              $ echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s OFS; ORS= s RS $1=$1 1'
              stack suffix over suffix flow suffix





              share|improve this answer






















                up vote
                2
                down vote










                up vote
                2
                down vote









                Another Awk variant - making use of the internal output separators to avoid an explicit loop:



                awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'


                ex.



                echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'
                stack suffix, over suffix, flow suffix


                For space separated output you can append the default output field separator to the suffix



                $ echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s OFS; ORS= s RS $1=$1 1'
                stack suffix over suffix flow suffix





                share|improve this answer












                Another Awk variant - making use of the internal output separators to avoid an explicit loop:



                awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'


                ex.



                echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s FS " "; ORS= s RS $1=$1 1'
                stack suffix, over suffix, flow suffix


                For space separated output you can append the default output field separator to the suffix



                $ echo 'stack,over,flow' | awk -F, -v s=' suffix' 'BEGINOFS= s OFS; ORS= s RS $1=$1 1'
                stack suffix over suffix flow suffix






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 at 22:14









                steeldriver

                33.8k34983




                33.8k34983




















                    up vote
                    1
                    down vote













                    awk -F , 'print $1" suffix", $2" suffix", $3" suffix"' OFS=", " suffix


                    awk -F , Sets the field separator as ,



                    print $1" suffix", and so on and so forth prints a space and then suffix after the value



                    OFS=", " Sets the output field separator as a comma with a space.



                    Output:



                     stack suffix, over suffix, flow suffix





                    share|improve this answer




















                    • Hey @Nasir, thanks, but in my case, there can be multiple values in input. So we need to use -F to get all values print with suffix.I don't know how to achieve this.
                      – SQLadmin
                      Nov 26 at 21:37










                    • @SQLadmin The other answer works better. In the future, you need to specify that information in your question so that it can be accounted for.
                      – Nasir Riley
                      Nov 26 at 21:54














                    up vote
                    1
                    down vote













                    awk -F , 'print $1" suffix", $2" suffix", $3" suffix"' OFS=", " suffix


                    awk -F , Sets the field separator as ,



                    print $1" suffix", and so on and so forth prints a space and then suffix after the value



                    OFS=", " Sets the output field separator as a comma with a space.



                    Output:



                     stack suffix, over suffix, flow suffix





                    share|improve this answer




















                    • Hey @Nasir, thanks, but in my case, there can be multiple values in input. So we need to use -F to get all values print with suffix.I don't know how to achieve this.
                      – SQLadmin
                      Nov 26 at 21:37










                    • @SQLadmin The other answer works better. In the future, you need to specify that information in your question so that it can be accounted for.
                      – Nasir Riley
                      Nov 26 at 21:54












                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    awk -F , 'print $1" suffix", $2" suffix", $3" suffix"' OFS=", " suffix


                    awk -F , Sets the field separator as ,



                    print $1" suffix", and so on and so forth prints a space and then suffix after the value



                    OFS=", " Sets the output field separator as a comma with a space.



                    Output:



                     stack suffix, over suffix, flow suffix





                    share|improve this answer












                    awk -F , 'print $1" suffix", $2" suffix", $3" suffix"' OFS=", " suffix


                    awk -F , Sets the field separator as ,



                    print $1" suffix", and so on and so forth prints a space and then suffix after the value



                    OFS=", " Sets the output field separator as a comma with a space.



                    Output:



                     stack suffix, over suffix, flow suffix






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 26 at 21:35









                    Nasir Riley

                    2,231239




                    2,231239











                    • Hey @Nasir, thanks, but in my case, there can be multiple values in input. So we need to use -F to get all values print with suffix.I don't know how to achieve this.
                      – SQLadmin
                      Nov 26 at 21:37










                    • @SQLadmin The other answer works better. In the future, you need to specify that information in your question so that it can be accounted for.
                      – Nasir Riley
                      Nov 26 at 21:54
















                    • Hey @Nasir, thanks, but in my case, there can be multiple values in input. So we need to use -F to get all values print with suffix.I don't know how to achieve this.
                      – SQLadmin
                      Nov 26 at 21:37










                    • @SQLadmin The other answer works better. In the future, you need to specify that information in your question so that it can be accounted for.
                      – Nasir Riley
                      Nov 26 at 21:54















                    Hey @Nasir, thanks, but in my case, there can be multiple values in input. So we need to use -F to get all values print with suffix.I don't know how to achieve this.
                    – SQLadmin
                    Nov 26 at 21:37




                    Hey @Nasir, thanks, but in my case, there can be multiple values in input. So we need to use -F to get all values print with suffix.I don't know how to achieve this.
                    – SQLadmin
                    Nov 26 at 21:37












                    @SQLadmin The other answer works better. In the future, you need to specify that information in your question so that it can be accounted for.
                    – Nasir Riley
                    Nov 26 at 21:54




                    @SQLadmin The other answer works better. In the future, you need to specify that information in your question so that it can be accounted for.
                    – Nasir Riley
                    Nov 26 at 21:54

















                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f484303%2fawk-print-all-values-with-a-suffix%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