LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

Reply
 
Search this Thread
Old 11-24-2010, 08:10 AM   #1
ghantauke
Member
 
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 08:12 AM.
 
Old 11-24-2010, 08:14 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
If the indices are numeric and sorted
Code:
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
Code:
for (i in array)
    print i, array[i]

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

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

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

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
For what it's worth, even if you have a multi-dimensional array a simple change to colucix's suggestion could be used:
Code:
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.
 
  


Reply


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 11:59 AM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
Awk output to bash array? kj6loh Programming 4 09-07-2009 12:36 PM
Best awk array tutorial bijit1709 Programming 1 01-25-2008 01:04 AM
How to return values into an array using awk Helene Programming 1 05-01-2004 10:05 PM


All times are GMT -5. The time now is 01:44 PM.

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