Awk command inside a for loop












1















I try, with no success, to use an awk command inside a for loop. I'v got a variable which contains a series of strings that I want to cut with awk to get the data. I know how to do that but what I really want is to cut the data successively.



So I've got this variable:



var="data1,data2,data3"


And here where I am right now:



for ((i=1; i<=3; i++))
do
echo $(awk -F, '{print $1}' <<<$var)
done


I try to replace the $1 by the loop $i but without success.









share









New contributor




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





















  • Every character in between a pair of single quotes is treated as a literal character.

    – Niko Gambt
    28 mins ago






  • 1





    What is your real use-case? looping over awk seems unnecessary here (for exaple in bash you could use a parameter substitution echo "${var//,/$'n'}")

    – steeldriver
    15 mins ago
















1















I try, with no success, to use an awk command inside a for loop. I'v got a variable which contains a series of strings that I want to cut with awk to get the data. I know how to do that but what I really want is to cut the data successively.



So I've got this variable:



var="data1,data2,data3"


And here where I am right now:



for ((i=1; i<=3; i++))
do
echo $(awk -F, '{print $1}' <<<$var)
done


I try to replace the $1 by the loop $i but without success.









share









New contributor




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





















  • Every character in between a pair of single quotes is treated as a literal character.

    – Niko Gambt
    28 mins ago






  • 1





    What is your real use-case? looping over awk seems unnecessary here (for exaple in bash you could use a parameter substitution echo "${var//,/$'n'}")

    – steeldriver
    15 mins ago














1












1








1








I try, with no success, to use an awk command inside a for loop. I'v got a variable which contains a series of strings that I want to cut with awk to get the data. I know how to do that but what I really want is to cut the data successively.



So I've got this variable:



var="data1,data2,data3"


And here where I am right now:



for ((i=1; i<=3; i++))
do
echo $(awk -F, '{print $1}' <<<$var)
done


I try to replace the $1 by the loop $i but without success.









share









New contributor




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












I try, with no success, to use an awk command inside a for loop. I'v got a variable which contains a series of strings that I want to cut with awk to get the data. I know how to do that but what I really want is to cut the data successively.



So I've got this variable:



var="data1,data2,data3"


And here where I am right now:



for ((i=1; i<=3; i++))
do
echo $(awk -F, '{print $1}' <<<$var)
done


I try to replace the $1 by the loop $i but without success.







shell-script awk





share









New contributor




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










share









New contributor




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








share



share








edited 17 mins ago









Rui F Ribeiro

39.6k1479132




39.6k1479132






New contributor




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









asked 42 mins ago









AmargeinAmargein

61




61




New contributor




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





New contributor





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






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













  • Every character in between a pair of single quotes is treated as a literal character.

    – Niko Gambt
    28 mins ago






  • 1





    What is your real use-case? looping over awk seems unnecessary here (for exaple in bash you could use a parameter substitution echo "${var//,/$'n'}")

    – steeldriver
    15 mins ago



















  • Every character in between a pair of single quotes is treated as a literal character.

    – Niko Gambt
    28 mins ago






  • 1





    What is your real use-case? looping over awk seems unnecessary here (for exaple in bash you could use a parameter substitution echo "${var//,/$'n'}")

    – steeldriver
    15 mins ago

















Every character in between a pair of single quotes is treated as a literal character.

– Niko Gambt
28 mins ago





Every character in between a pair of single quotes is treated as a literal character.

– Niko Gambt
28 mins ago




1




1





What is your real use-case? looping over awk seems unnecessary here (for exaple in bash you could use a parameter substitution echo "${var//,/$'n'}")

– steeldriver
15 mins ago





What is your real use-case? looping over awk seems unnecessary here (for exaple in bash you could use a parameter substitution echo "${var//,/$'n'}")

– steeldriver
15 mins ago










3 Answers
3






active

oldest

votes


















3














You can accomplish what you're trying to do by using double quotes in the awk script to inject the shell variable into it. You still want to keep one literal $ in it, which you can do by escaping it with backslash:



echo $(awk -F, "{print $$i}" <<<$var)


This will expand the $i to 1, 2 and 3 in each of the iterations, therefore awk will see $1, $2 and $3 which will make it expand each of the fields.



Another possibility is to inject the shell variable as an awk variable using the -v flag:



echo $(awk -F, -v i="$i" '{print $i}' <<<$var)


That injects the variable into awk with the same name. Variables in awk don't use a $, which is used for fields, so $i is enough to refer to the i-th field if i is a variable in awk.



Injecting a variable with -v is generally a safer approach, particularly when it can contain arbitrary sequences of characters, in that case there's less risk that the contents will be executed as awk code against your intentions. But since in your case the variable holds a single integer, that's less of a concern.



Yet another option is to use a for loop in awk itself. See awk documentation (or search this site) for more details on how to do that.






share|improve this answer





















  • 1





    ... or set the input record separator appropriately awk -v RS='[,n]' 1 <<< "$var" (or maybe more portably printf "$var" | awk -v RS=, 1 )

    – steeldriver
    13 mins ago











  • @steeldriver That awk script will print all three fields at once. Which is ok given the OP is doing just that... I ended up focusing the answer on extracting a single field on the assumption they were interested in executing other commands in the shell loop (which might have been the motivation for using one in the first place...)

    – filbranden
    6 mins ago






  • 1





    Understood - that's why I commented on the OP to clarify what they really want to do ;)

    – steeldriver
    3 mins ago



















0














Using awk seems excessive in this circumstance, how about a tr and a while-loop:



tr , 'n' <<<"$var" | while read; do
echo $REPLY
done


Output:



data1
data2
data3




share































    0














    awk can accept both j (as variable) and $j (as field index):



    for i in [1-3]; do echo $var | awk -v j=$i -F , '{print $j}'; done


    $i in the example "confused" awk which one to use (shell or its own variable - taking precedence) as both are referred to with $ prefix.



    note



    sh shell which is standard for "portable" scripting do not support the:



    (( i=1; i<=3; i++; )) and <<< $var constructs






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


      }
      });






      Amargein 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%2f496994%2fawk-command-inside-a-for-loop%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









      3














      You can accomplish what you're trying to do by using double quotes in the awk script to inject the shell variable into it. You still want to keep one literal $ in it, which you can do by escaping it with backslash:



      echo $(awk -F, "{print $$i}" <<<$var)


      This will expand the $i to 1, 2 and 3 in each of the iterations, therefore awk will see $1, $2 and $3 which will make it expand each of the fields.



      Another possibility is to inject the shell variable as an awk variable using the -v flag:



      echo $(awk -F, -v i="$i" '{print $i}' <<<$var)


      That injects the variable into awk with the same name. Variables in awk don't use a $, which is used for fields, so $i is enough to refer to the i-th field if i is a variable in awk.



      Injecting a variable with -v is generally a safer approach, particularly when it can contain arbitrary sequences of characters, in that case there's less risk that the contents will be executed as awk code against your intentions. But since in your case the variable holds a single integer, that's less of a concern.



      Yet another option is to use a for loop in awk itself. See awk documentation (or search this site) for more details on how to do that.






      share|improve this answer





















      • 1





        ... or set the input record separator appropriately awk -v RS='[,n]' 1 <<< "$var" (or maybe more portably printf "$var" | awk -v RS=, 1 )

        – steeldriver
        13 mins ago











      • @steeldriver That awk script will print all three fields at once. Which is ok given the OP is doing just that... I ended up focusing the answer on extracting a single field on the assumption they were interested in executing other commands in the shell loop (which might have been the motivation for using one in the first place...)

        – filbranden
        6 mins ago






      • 1





        Understood - that's why I commented on the OP to clarify what they really want to do ;)

        – steeldriver
        3 mins ago
















      3














      You can accomplish what you're trying to do by using double quotes in the awk script to inject the shell variable into it. You still want to keep one literal $ in it, which you can do by escaping it with backslash:



      echo $(awk -F, "{print $$i}" <<<$var)


      This will expand the $i to 1, 2 and 3 in each of the iterations, therefore awk will see $1, $2 and $3 which will make it expand each of the fields.



      Another possibility is to inject the shell variable as an awk variable using the -v flag:



      echo $(awk -F, -v i="$i" '{print $i}' <<<$var)


      That injects the variable into awk with the same name. Variables in awk don't use a $, which is used for fields, so $i is enough to refer to the i-th field if i is a variable in awk.



      Injecting a variable with -v is generally a safer approach, particularly when it can contain arbitrary sequences of characters, in that case there's less risk that the contents will be executed as awk code against your intentions. But since in your case the variable holds a single integer, that's less of a concern.



      Yet another option is to use a for loop in awk itself. See awk documentation (or search this site) for more details on how to do that.






      share|improve this answer





















      • 1





        ... or set the input record separator appropriately awk -v RS='[,n]' 1 <<< "$var" (or maybe more portably printf "$var" | awk -v RS=, 1 )

        – steeldriver
        13 mins ago











      • @steeldriver That awk script will print all three fields at once. Which is ok given the OP is doing just that... I ended up focusing the answer on extracting a single field on the assumption they were interested in executing other commands in the shell loop (which might have been the motivation for using one in the first place...)

        – filbranden
        6 mins ago






      • 1





        Understood - that's why I commented on the OP to clarify what they really want to do ;)

        – steeldriver
        3 mins ago














      3












      3








      3







      You can accomplish what you're trying to do by using double quotes in the awk script to inject the shell variable into it. You still want to keep one literal $ in it, which you can do by escaping it with backslash:



      echo $(awk -F, "{print $$i}" <<<$var)


      This will expand the $i to 1, 2 and 3 in each of the iterations, therefore awk will see $1, $2 and $3 which will make it expand each of the fields.



      Another possibility is to inject the shell variable as an awk variable using the -v flag:



      echo $(awk -F, -v i="$i" '{print $i}' <<<$var)


      That injects the variable into awk with the same name. Variables in awk don't use a $, which is used for fields, so $i is enough to refer to the i-th field if i is a variable in awk.



      Injecting a variable with -v is generally a safer approach, particularly when it can contain arbitrary sequences of characters, in that case there's less risk that the contents will be executed as awk code against your intentions. But since in your case the variable holds a single integer, that's less of a concern.



      Yet another option is to use a for loop in awk itself. See awk documentation (or search this site) for more details on how to do that.






      share|improve this answer















      You can accomplish what you're trying to do by using double quotes in the awk script to inject the shell variable into it. You still want to keep one literal $ in it, which you can do by escaping it with backslash:



      echo $(awk -F, "{print $$i}" <<<$var)


      This will expand the $i to 1, 2 and 3 in each of the iterations, therefore awk will see $1, $2 and $3 which will make it expand each of the fields.



      Another possibility is to inject the shell variable as an awk variable using the -v flag:



      echo $(awk -F, -v i="$i" '{print $i}' <<<$var)


      That injects the variable into awk with the same name. Variables in awk don't use a $, which is used for fields, so $i is enough to refer to the i-th field if i is a variable in awk.



      Injecting a variable with -v is generally a safer approach, particularly when it can contain arbitrary sequences of characters, in that case there's less risk that the contents will be executed as awk code against your intentions. But since in your case the variable holds a single integer, that's less of a concern.



      Yet another option is to use a for loop in awk itself. See awk documentation (or search this site) for more details on how to do that.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 25 mins ago

























      answered 32 mins ago









      filbrandenfilbranden

      7,84521038




      7,84521038








      • 1





        ... or set the input record separator appropriately awk -v RS='[,n]' 1 <<< "$var" (or maybe more portably printf "$var" | awk -v RS=, 1 )

        – steeldriver
        13 mins ago











      • @steeldriver That awk script will print all three fields at once. Which is ok given the OP is doing just that... I ended up focusing the answer on extracting a single field on the assumption they were interested in executing other commands in the shell loop (which might have been the motivation for using one in the first place...)

        – filbranden
        6 mins ago






      • 1





        Understood - that's why I commented on the OP to clarify what they really want to do ;)

        – steeldriver
        3 mins ago














      • 1





        ... or set the input record separator appropriately awk -v RS='[,n]' 1 <<< "$var" (or maybe more portably printf "$var" | awk -v RS=, 1 )

        – steeldriver
        13 mins ago











      • @steeldriver That awk script will print all three fields at once. Which is ok given the OP is doing just that... I ended up focusing the answer on extracting a single field on the assumption they were interested in executing other commands in the shell loop (which might have been the motivation for using one in the first place...)

        – filbranden
        6 mins ago






      • 1





        Understood - that's why I commented on the OP to clarify what they really want to do ;)

        – steeldriver
        3 mins ago








      1




      1





      ... or set the input record separator appropriately awk -v RS='[,n]' 1 <<< "$var" (or maybe more portably printf "$var" | awk -v RS=, 1 )

      – steeldriver
      13 mins ago





      ... or set the input record separator appropriately awk -v RS='[,n]' 1 <<< "$var" (or maybe more portably printf "$var" | awk -v RS=, 1 )

      – steeldriver
      13 mins ago













      @steeldriver That awk script will print all three fields at once. Which is ok given the OP is doing just that... I ended up focusing the answer on extracting a single field on the assumption they were interested in executing other commands in the shell loop (which might have been the motivation for using one in the first place...)

      – filbranden
      6 mins ago





      @steeldriver That awk script will print all three fields at once. Which is ok given the OP is doing just that... I ended up focusing the answer on extracting a single field on the assumption they were interested in executing other commands in the shell loop (which might have been the motivation for using one in the first place...)

      – filbranden
      6 mins ago




      1




      1





      Understood - that's why I commented on the OP to clarify what they really want to do ;)

      – steeldriver
      3 mins ago





      Understood - that's why I commented on the OP to clarify what they really want to do ;)

      – steeldriver
      3 mins ago













      0














      Using awk seems excessive in this circumstance, how about a tr and a while-loop:



      tr , 'n' <<<"$var" | while read; do
      echo $REPLY
      done


      Output:



      data1
      data2
      data3




      share




























        0














        Using awk seems excessive in this circumstance, how about a tr and a while-loop:



        tr , 'n' <<<"$var" | while read; do
        echo $REPLY
        done


        Output:



        data1
        data2
        data3




        share


























          0












          0








          0







          Using awk seems excessive in this circumstance, how about a tr and a while-loop:



          tr , 'n' <<<"$var" | while read; do
          echo $REPLY
          done


          Output:



          data1
          data2
          data3




          share













          Using awk seems excessive in this circumstance, how about a tr and a while-loop:



          tr , 'n' <<<"$var" | while read; do
          echo $REPLY
          done


          Output:



          data1
          data2
          data3





          share











          share


          share










          answered 4 mins ago









          ThorThor

          11.7k13459




          11.7k13459























              0














              awk can accept both j (as variable) and $j (as field index):



              for i in [1-3]; do echo $var | awk -v j=$i -F , '{print $j}'; done


              $i in the example "confused" awk which one to use (shell or its own variable - taking precedence) as both are referred to with $ prefix.



              note



              sh shell which is standard for "portable" scripting do not support the:



              (( i=1; i<=3; i++; )) and <<< $var constructs






              share|improve this answer






























                0














                awk can accept both j (as variable) and $j (as field index):



                for i in [1-3]; do echo $var | awk -v j=$i -F , '{print $j}'; done


                $i in the example "confused" awk which one to use (shell or its own variable - taking precedence) as both are referred to with $ prefix.



                note



                sh shell which is standard for "portable" scripting do not support the:



                (( i=1; i<=3; i++; )) and <<< $var constructs






                share|improve this answer




























                  0












                  0








                  0







                  awk can accept both j (as variable) and $j (as field index):



                  for i in [1-3]; do echo $var | awk -v j=$i -F , '{print $j}'; done


                  $i in the example "confused" awk which one to use (shell or its own variable - taking precedence) as both are referred to with $ prefix.



                  note



                  sh shell which is standard for "portable" scripting do not support the:



                  (( i=1; i<=3; i++; )) and <<< $var constructs






                  share|improve this answer















                  awk can accept both j (as variable) and $j (as field index):



                  for i in [1-3]; do echo $var | awk -v j=$i -F , '{print $j}'; done


                  $i in the example "confused" awk which one to use (shell or its own variable - taking precedence) as both are referred to with $ prefix.



                  note



                  sh shell which is standard for "portable" scripting do not support the:



                  (( i=1; i<=3; i++; )) and <<< $var constructs







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 4 mins ago

























                  answered 13 mins ago









                  w17tw17t

                  676522




                  676522






















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










                      draft saved

                      draft discarded


















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













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












                      Amargein 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%2f496994%2fawk-command-inside-a-for-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

                      What are all the squawk codes?

                      What are differences between VBoxVGA, VMSVGA and VBoxSVGA in VirtualBox?

                      Hudsonelva