6

My question is, is it possible to assign a bash variable within an awk script.

i.e Assuming following is a shell script I have written,

declare -A sample_associative_array
awk -F'=' '{$sample_associative_array[$2]=$1}'  /tmp/sample.txt

given /tmp/sample.txt has:

abc=0
def=1

I tried

echo $sample_associative_array[0]

and it doesnt work.

Any help would be appreciated.

Cheers!

3 Answers 3

9

Not quite the same thing, but you can have awk output strings that can be used by the bash built-in command declare to populate an associative array. I'm not entirely sure why you would want to do this, though.

$ declare $( awk -F'=' '{print "aa["$2"]="$1}' sample.txt )
$ echo ${aa[0]}
abc
$ echo ${aa[1]}
def
Sign up to request clarification or add additional context in comments.

1 Comment

I wanted to read a file and get an associative array out of it. And from my experience, I felt awk would be a natural candidate for this. Hence I went that way. But now, I'm going for the usual parameter expansion option provided by bash. I'm doing a prefix and suffix match on the data and populating my associative array. Thanks for the alternative method though. Appreciate it!
1

No. A child process cannot assign to a variable in the parent, you would have this issue regardless of the language. awk cannot even directly read a bash associative array either, since you cannot export an array from bash (or any other shell that I know of).

You will always get these kinds of problems when trying to mix languages. General tip is to write the whole lot in either bash or awk, both are quite powerful.

1 Comment

Well explained! Looking at this in terms of process makes it a bit clearer.
0

You can't do that. Awk and Bash use totally different scopes of variables, and allowing one to use the variables declared in another one would be a great violation of process sovereignty.

What you're trying to do can be very simply done with Bash via using parameter expansion with removal of the prefix/suffix, e.g. ${parameter#word} and ${parameter%word}. Consult the Bash manual for more information.

Also, array references in Bash use other syntax : ${sample_associative_array[0]} is what you want.

3 Comments

Thanks for the response. Yes, regarding echo ${sample_array[0]}, I knew that but somehow missed it out. Can you tell me what I need to search for regarding this parameter expansion? It would be great if you could provide an example. Cheers!
Okay, Got it! Thanks a lot for the answer! For those who are looking for this kind of match, here is a nice article: linuxjournal.com/content/bash-parameter-expansion
@Unsung: Search man bash for Arrays: Any element of an array may be referenced using ${name[subscript]}.

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.