0

I am planning to use a multidimensional array in awk and I am storing the details in the format as below

CODE

arr[customer1,account1]
arr[customer1,account2]
arr[customer2,account1]
arr[customer2,account2]

DETAILS

I just need to know, how would I be able to traverse this and print the results. Also I am using this awk inside a shell script program so I would like to know will I be able to use the same array inside the shell script, if so how to transfer this. I searched in many sites, I am not able to find a correct solution for this. Could you ppl plz help me. Thank you.

1
  • An array can't be transferred from AWK to the shell. You can print the values from the AWK script and read them into an array in the shell (such as Bash, which supports arrays). Note that Bash doesn't support multidimensional arrays without ugly hacks and that before version 4 it doesn't support associative arrays. Or you can write the whole script in AWK and have complete access to the arrays. Commented Jun 24, 2012 at 13:50

1 Answer 1

1

Keys from multidimensional arrays are separated with the value of SUBSEP variable, use it to split them. Traverse the array like this:

for ( key in arr ) {
    split( key, values, SUBSEP )
    ## values[1] -> First value of the multidimensional array.
    ## values[2] -> Second value of the multidimensional array.
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer. In mycase I dint change my subsep hence I assume it to be space . I am using like the below awk '{FS = "=";if ($1 ~ /Hi/) {customer=$2;} else if ($1 ~ /bye/) {split($2,g,"[\"]");h[customer, g[2]]++;} END {for (key in arr){ split(key,values," ") print values[1],values[2],h[values[1],values[2]]}}... But I am getting an error in this ..
@User: I can't see your error but SUBSEP is not a space by default. It is \034, an uncommon and not visible char.
So can I replace space with \034. And is my approach of printing the value correct ...
Actually I am not getting the value of third parameter I mean h[values[1],values[2]] which is the count ...Any suggestions pls
@User: Don't use \034 literally, use the variable SUBSEP.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.