Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-24-2010, 09:10 AM   #1
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
printing array in awk

is there anyway i can print out all elements of an array in awk without knowing what the indices are? or even indices and elements pairs for that matter. i thought about using a for loop but without knowing what the indices are i don't know how to do it.

Last edited by ghantauke; 11-24-2010 at 09:12 AM.
Old 11-24-2010, 09:14 AM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
If the indices are numeric and sorted
for (i = 1; i <= length(array); i++)
    print i, array[i]
Actually the length function was implemented to return the length of a string. Since gawk version 3.1.5, if the argument is an array, it returns the number of elements stored in the array.

If the indices are made of unsorted alphanumeric strings you can simply do
for (i in array)
    print i, array[i]

Last edited by colucix; 11-24-2010 at 09:19 AM.
Old 11-24-2010, 09:20 AM   #3
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
nevermind guess you edited your post ^^

Last edited by ghantauke; 11-24-2010 at 09:21 AM.
Old 11-24-2010, 10:16 AM   #4
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 347Reputation: 347Reputation: 347Reputation: 347
For what it's worth, even if you have a multi-dimensional array a simple change to colucix's suggestion could be used:
for (index in array) {
    readable_index=gensub(SUBSEP,", ","G",index)
    print "array["readable_index"] = " array[index]
You might want to look at the asort and asorti functions if the order of the array elements is of significance.

Note: Some of the above is specific to gawk, and may not be portable.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to get search results to array by awk webhope Programming 5 05-05-2010 12:59 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 11:07 AM
Awk output to bash array? kj6loh Programming 4 09-07-2009 01:36 PM
Best awk array tutorial bijit1709 Programming 1 01-25-2008 02:04 AM
How to return values into an array using awk Helene Programming 1 05-01-2004 11:05 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:33 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration