Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i'm using awk inside bash. i've got an array in awk called arrayinawk. everytime i call another awk command in bash i have to keep creating arrayinawk to work with it. is there anyway i can store arrayinawk in bash and just call the stored value next time i use awk?
Last edited by ghantauke; 11-24-2010 at 09:01 AM.
Click here to see the post LQ members have rated as the most helpful post in this thread.
You can certainly pass the array back to bash by having awk write a bash array assignment command and having bash eval the awk output. AFAIK awk's -v option only allows passing single values but the container bash could put awk statements to load the arrayin the awk script.
You can certainly pass the array back to bash by having awk write a bash array assignment command and having bash eval the awk output. AFAIK awk's -v option only allows passing single values but the container bash could put awk statements to load the arrayin the awk script.
I'm using awk inside bash. I'm working with the file called test which has the following content.
kill bill
epic deal
blonde chick
linux questions
I store the data into an array called dataarray.
awk '{dataarray[$1]=$2}' test
Later on when I want to call this array in another awk command line. So I want to store dataarray into a bash array and call it in the other awk command line. How can I do this? Thanks in advance.
This seems particularly convoluted? Is there a reason you cannot either extend the first awk script to perform the task of the one you wish to utilize the array?
Or maybe even just create the initial array in bash (v4 does associative arrays) and then use bash to perform all your tasks?
This seems particularly convoluted? Is there a reason you cannot either extend the first awk script to perform the task of the one you wish to utilize the array?
Or maybe even just create the initial array in bash (v4 does associative arrays) and then use bash to perform all your tasks?
I cannot extend the awk script to perform the whole task because I need to use bash commands on the array later on.
For the second question I simple don't know how to store the values in bash array without using awk. It'd help if you could give me a hint on how to do it.
bash: =kill: command not found
bash: =bill: command not found
bash: =epic: command not found
bash: =deal: command not found
bash: =blonde: command not found
bash: =chick: command not found
bash: =linux: command not found
bash: =questions: command not found
And in any event, even if this works after someone points out what I'm doing wrong, I agree with catkin - there's probably an easier way to either use just awk, or just bash. IF not, that's fine, but please humor us: can you give us a larger view of the entire process requirements here? I.e. you want to process this file into an array in awk (and presumably do something to the data) but then you also want the same array, in bash. I think there must be a compromise or different perspective that maybe can be taken, but to suggest, we need to know what's going on in the larger picture.
You can certainly pass the array back to bash by having awk write a bash array assignment command and having bash eval the awk output. AFAIK awk's -v option only allows passing single values but the container bash could put awk statements to load the arrayin the awk script.
i'm not sure what mean by "having bash eval the awk output"
if you mean using eval command like the following:
eval array[0]=awk...........
then it doesn't work since eval only works on variables and not on commands like awk.
You want A= not $A= (think of the $ as meaning "the value of") and you need to declare the array as associative
Code:
#!/bin/bash
# Configure script environment
set -o nounset
unalias -a
# Read data
declare -A array
while read index data
do
indexes+=( $index )
array[$index]=$data
done < LQ-846269.txt
# Show array content
for ((i=0; i<${#indexes[*]}; i++))
do
index=${indexes[i]}
echo "array[$index] is ${array[$index]}"
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.