How to view all the content in an awk array?











up vote
29
down vote

favorite
4












In my understanding, awk array is something like python dict.



So I write down the code bellow to explore it:



awk '{my_dict[$1] = $2} END { print my_dict}' zen


And I got: awk: can't read value of my_dict; it's an array name.



As the first column isn`t a number, how could I read the total content of the array or traverse it?










share|improve this question


























    up vote
    29
    down vote

    favorite
    4












    In my understanding, awk array is something like python dict.



    So I write down the code bellow to explore it:



    awk '{my_dict[$1] = $2} END { print my_dict}' zen


    And I got: awk: can't read value of my_dict; it's an array name.



    As the first column isn`t a number, how could I read the total content of the array or traverse it?










    share|improve this question
























      up vote
      29
      down vote

      favorite
      4









      up vote
      29
      down vote

      favorite
      4






      4





      In my understanding, awk array is something like python dict.



      So I write down the code bellow to explore it:



      awk '{my_dict[$1] = $2} END { print my_dict}' zen


      And I got: awk: can't read value of my_dict; it's an array name.



      As the first column isn`t a number, how could I read the total content of the array or traverse it?










      share|improve this question













      In my understanding, awk array is something like python dict.



      So I write down the code bellow to explore it:



      awk '{my_dict[$1] = $2} END { print my_dict}' zen


      And I got: awk: can't read value of my_dict; it's an array name.



      As the first column isn`t a number, how could I read the total content of the array or traverse it?







      awk






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 6 '15 at 10:28









      Zen

      2,22293054




      2,22293054






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          43
          down vote



          accepted










          You can loop over the array's keys and extract the corresponding values:



          awk '{my_dict[$1] = $2} END { for (key in my_dict) { print my_dict[key] } }' zen


          To get output similar to that you'd get with a Python dictionary, you can print the key as well:



          awk '{my_dict[$1] = $2} END { for (key in my_dict) { print key ": " my_dict[key] } }' zen


          This works regardless of the key type.






          share|improve this answer






























            up vote
            10
            down vote













            That would loop through the array:



            END { for (i in my_dict) print my_dict[i] }


            i is the index.






            share|improve this answer




























              up vote
              6
              down vote













              Array in awk is not first class object like dictionary in Python. In awk, array name without subscript can only use in two context:




              • A parameter in a function definition or function call.

              • Name token after keyword in.


              In other context, awk will raise an error.



              You need a for loop to iterate and print content of an array:



              $ echo 1 2 | awk '{my_dict[$1] = $2};END {for(i in my_dict) print my_dict[i]}'
              2





              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',
                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%2f183279%2fhow-to-view-all-the-content-in-an-awk-array%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
                43
                down vote



                accepted










                You can loop over the array's keys and extract the corresponding values:



                awk '{my_dict[$1] = $2} END { for (key in my_dict) { print my_dict[key] } }' zen


                To get output similar to that you'd get with a Python dictionary, you can print the key as well:



                awk '{my_dict[$1] = $2} END { for (key in my_dict) { print key ": " my_dict[key] } }' zen


                This works regardless of the key type.






                share|improve this answer



























                  up vote
                  43
                  down vote



                  accepted










                  You can loop over the array's keys and extract the corresponding values:



                  awk '{my_dict[$1] = $2} END { for (key in my_dict) { print my_dict[key] } }' zen


                  To get output similar to that you'd get with a Python dictionary, you can print the key as well:



                  awk '{my_dict[$1] = $2} END { for (key in my_dict) { print key ": " my_dict[key] } }' zen


                  This works regardless of the key type.






                  share|improve this answer

























                    up vote
                    43
                    down vote



                    accepted







                    up vote
                    43
                    down vote



                    accepted






                    You can loop over the array's keys and extract the corresponding values:



                    awk '{my_dict[$1] = $2} END { for (key in my_dict) { print my_dict[key] } }' zen


                    To get output similar to that you'd get with a Python dictionary, you can print the key as well:



                    awk '{my_dict[$1] = $2} END { for (key in my_dict) { print key ": " my_dict[key] } }' zen


                    This works regardless of the key type.






                    share|improve this answer














                    You can loop over the array's keys and extract the corresponding values:



                    awk '{my_dict[$1] = $2} END { for (key in my_dict) { print my_dict[key] } }' zen


                    To get output similar to that you'd get with a Python dictionary, you can print the key as well:



                    awk '{my_dict[$1] = $2} END { for (key in my_dict) { print key ": " my_dict[key] } }' zen


                    This works regardless of the key type.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 6 '15 at 13:08

























                    answered Feb 6 '15 at 10:34









                    Stephen Kitt

                    160k24357432




                    160k24357432
























                        up vote
                        10
                        down vote













                        That would loop through the array:



                        END { for (i in my_dict) print my_dict[i] }


                        i is the index.






                        share|improve this answer

























                          up vote
                          10
                          down vote













                          That would loop through the array:



                          END { for (i in my_dict) print my_dict[i] }


                          i is the index.






                          share|improve this answer























                            up vote
                            10
                            down vote










                            up vote
                            10
                            down vote









                            That would loop through the array:



                            END { for (i in my_dict) print my_dict[i] }


                            i is the index.






                            share|improve this answer












                            That would loop through the array:



                            END { for (i in my_dict) print my_dict[i] }


                            i is the index.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 6 '15 at 10:35









                            chaos

                            34.9k773115




                            34.9k773115






















                                up vote
                                6
                                down vote













                                Array in awk is not first class object like dictionary in Python. In awk, array name without subscript can only use in two context:




                                • A parameter in a function definition or function call.

                                • Name token after keyword in.


                                In other context, awk will raise an error.



                                You need a for loop to iterate and print content of an array:



                                $ echo 1 2 | awk '{my_dict[$1] = $2};END {for(i in my_dict) print my_dict[i]}'
                                2





                                share|improve this answer



























                                  up vote
                                  6
                                  down vote













                                  Array in awk is not first class object like dictionary in Python. In awk, array name without subscript can only use in two context:




                                  • A parameter in a function definition or function call.

                                  • Name token after keyword in.


                                  In other context, awk will raise an error.



                                  You need a for loop to iterate and print content of an array:



                                  $ echo 1 2 | awk '{my_dict[$1] = $2};END {for(i in my_dict) print my_dict[i]}'
                                  2





                                  share|improve this answer

























                                    up vote
                                    6
                                    down vote










                                    up vote
                                    6
                                    down vote









                                    Array in awk is not first class object like dictionary in Python. In awk, array name without subscript can only use in two context:




                                    • A parameter in a function definition or function call.

                                    • Name token after keyword in.


                                    In other context, awk will raise an error.



                                    You need a for loop to iterate and print content of an array:



                                    $ echo 1 2 | awk '{my_dict[$1] = $2};END {for(i in my_dict) print my_dict[i]}'
                                    2





                                    share|improve this answer














                                    Array in awk is not first class object like dictionary in Python. In awk, array name without subscript can only use in two context:




                                    • A parameter in a function definition or function call.

                                    • Name token after keyword in.


                                    In other context, awk will raise an error.



                                    You need a for loop to iterate and print content of an array:



                                    $ echo 1 2 | awk '{my_dict[$1] = $2};END {for(i in my_dict) print my_dict[i]}'
                                    2






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Apr 28 '15 at 19:28

























                                    answered Feb 6 '15 at 10:40









                                    cuonglm

                                    101k23197299




                                    101k23197299






























                                        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%2f183279%2fhow-to-view-all-the-content-in-an-awk-array%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

                                        Morgemoulin

                                        Scott Moir

                                        Souastre