awk array to variable code example
Example: awk: can't read value of a; it's an array name.
# Error:
awk: can't read value of a; it's an array name.
# Solution:
# This error usually comes up if you're trying to print the keys or
# values from an awk array (i.e. dictionary) incorrectly. To do this
# properly, you need to loop over the keys in your array.
# Example usage for printing keys:
awk '{ a[$1]=$2 } END { for (key in a) { print key } }' input_file
# Where:
# - The above statements prints all keys in the awk array/dictionary
# made from the input_file
# - a[$1] is an array/dictionary in which the first column ($1) is
# used as the key and the second column ($2) is used as the value
# - END indicates what to do after the first statement has finished
# running. Here we start a loop that loops through the keys in the
# array and prints them.
# Example usage for printing key values:
awk '{ a[$1]=$2 } END { for (key in a) { print a[key] } }' input_file
# Where:
# - The above statements prints all key values in the awk
# array/dictionary made from the input_file