How to view all the content in an awk array?
up vote
29
down vote
favorite
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
add a comment |
up vote
29
down vote
favorite
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
add a comment |
up vote
29
down vote
favorite
up vote
29
down vote
favorite
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
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
awk
asked Feb 6 '15 at 10:28
Zen
2,22293054
2,22293054
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Feb 6 '15 at 13:08
answered Feb 6 '15 at 10:34
Stephen Kitt
160k24357432
160k24357432
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
That would loop through the array:
END { for (i in my_dict) print my_dict[i] }
i
is the index.
answered Feb 6 '15 at 10:35
chaos
34.9k773115
34.9k773115
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Apr 28 '15 at 19:28
answered Feb 6 '15 at 10:40
cuonglm
101k23197299
101k23197299
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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