Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-17-2008, 03:33 PM   #1
LQ Newbie
Registered: Nov 2008
Posts: 9

Rep: Reputation: 0
how to implement bidimensional arrays in awk?

hello everyone!!! a have some really big question marks and i hope u can give me some answers!
lets say i have an input file like this :
a b d g
a b c d e f g h
a c d f g h
a b c d g

i need to arrange the fields in an output file like this:
a b - d - - g -
a b c d e f g h
a - c d - f g h
a b c d - - g -

for this i came up with the script below :

awk ; { if(max_nf<NF)
END {for (i=1;i<=max_nr;i++)
{if (j==1 && v[j,i]!="a")
if (j==8 && v[j,i]!="a")
printf("%s ",v[j,i]);

Now, if i run the script on a Ubuntu system it works perfectly!
But i really HAVE to run it on a Solaris 5.9 and there is the problem!!!
Apparently i has "syntax errors" at assigning v[i,Nr]=$i, and at all "for" loops .
So, it's not a code problem, but a syntax one.
The thing is both systems have /usr/bin/awk and /usr/bin/nawk.
How can i find out what my version of awk can support and what it can't?
And more importantly, how do i rewrite the script in order to work on the Solaris?
Hope anyone cand help me
Old 11-17-2008, 05:08 PM   #2
Registered: Dec 2002
Location: Au
Distribution: Debian
Posts: 209

Rep: Reputation: 54
Solaris has old school versions of such utilities.
Linux has the latest, greatest GNU versions.
On my system awk is symlinked to mawk for example.

If your script won't run under nawk then try gawk.
You can get gawk for solaris from

The awk lineage goes something like:
what's next, squawk???

WP has some history, if you're interested:

Last edited by yowi; 11-17-2008 at 05:12 PM.
Old 11-18-2008, 03:25 AM   #3
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
GNU awk does not have multi-dimensional arrays. It simulates multi-dimensional arrays by concatenating the indices and using the result as an index for a one-dimensional array. You can easily do this yourself by writing

array["1" "," "2"]
instead of

array[1, 2]
Old 11-18-2008, 06:54 AM   #4
LQ Newbie
Registered: Nov 2008
Posts: 9

Original Poster
Rep: Reputation: 0
thank u both for your suggestion...
i don't have permission to update the system , so i can't get gawk.
i will try instead simulating the multi dimensional array.
i'' be back to tell u if it worked.
thanks again!



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
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
Some comments on awk and awk scripts makyo Programming 4 03-02-2008 05:39 PM
Arrays of Structures to Arrays of Classes knobby67 Programming 1 01-01-2008 01:39 PM
Passing bidimensional arrays to function in C pittopitto Programming 5 05-18-2006 09:31 AM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM

All times are GMT -5. The time now is 10:07 AM.

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