How to view all the content in an awk array?

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?

Asked By: Zen


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.

Answered By: Stephen Kitt

That would loop through the array:

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

i is the index.

Answered By: chaos

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]}'
Answered By: cuonglm

GNU AWK uses multidimensional associative arrays such as:



Sometimes it’s hard to explore awk arrays such as PROCINFO (because in addition to scalars it has also subarrays: argv & identifiers)

So I wrote this awk script for myself. Maybe it will be useful for you. You can remove such substrings: 33[1;31m if you don’t like/have colored output.

#!/usr/bin/gawk -f
function arraytree(inputarray,arrname,i,member,arrnum){
  print indent arrname member "[33[1;31m" i "33[0m]="33[1;32m" inputarray"33[0m""
  member=i?member "[" i "]":""
  print indent (i? arrname member:arrname)"(33[1;34m" length(inputarray) " member"(length(inputarray)>1?"s":"")"33[0m)"
  indent=indent " "
  for(i in inputarray){
Answered By: Evgeny
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.