LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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, 09:55 AM   #1
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
storing array from awk in bash


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 10:01 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-24-2010, 10:08 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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.
 
Old 11-24-2010, 12:06 PM   #3
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by catkin View Post
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 don't really follow that ^^
 
Old 11-24-2010, 12:31 PM   #4
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Saving awk array into bash

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.
 
Old 11-24-2010, 06:25 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
What he's saying is that there's no simple way.
 
Old 11-24-2010, 08:14 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,244

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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?
 
Old 11-24-2010, 08:49 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
I merged your two threads; don't go producing dupes because you don't
like the answers in the original.
 
2 members found this post helpful.
Old 11-24-2010, 09:32 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by Tinkster View Post
I merged your two threads; don't go producing dupes because you don't
like the answers in the original.
Thanks Tinkster, that simplifies things
 
Old 11-25-2010, 07:28 AM   #9
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by grail View Post
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.
 
Old 11-25-2010, 07:53 AM   #10
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
I tried the following for assigning the file to an associative array in bash. It doesn't work.

lines=$(wc -l < file)
for ((i=1;lines>=i;i++))
do
$A=$(head -$i file| tail -1 | awk '{print $1}')
$B=$(head -$i file| tail -1 | awk '{print $2}')
arraytest[$A]=$B
done

The error message.

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

I can't see where I'm going wrong.
 
Old 11-25-2010, 08:12 AM   #11
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
I tried doing this:
Code:
awk '$1!=""{printf("%s","dataarray[\""$1"\"]=\""$2"\";")}END{print}' test
which produces this:
Code:
dataarray["kill"]="bill";dataarray["epic"]="deal";dataarray["blonde"]="chick";dataarray["linux"]="questions";
which you can eval:
Code:
root@reactor: eval $(awk '$1!=""{printf("%s","dataarray[\""$1"\"]=\""$2"\";")}END{print}' test)
Executes without error; however, the array does not seem to be created properly:
Code:
root@reactor: echo ${dataarray[*]}
questions
root@reactor:
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.
 
Old 11-25-2010, 08:23 AM   #12
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by catkin View Post
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.
 
Old 11-25-2010, 08:32 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
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
 
Old 11-25-2010, 08:36 AM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by GrapefruiTgirl View Post
And in any event, even if this works after someone points out what I'm doing wrong ...
Thanks for explaining that Sasha

The missing step was declaring dattaarray as associative:
Code:
c@CW8:~$ dataarray["kill"]="bill";dataarray["epic"]="deal";dataarray["blonde"]="chick";dataarray["linux"]="questions";
c@CW8:~$ echo ${dataarray[*]}
questions
c@CW8:~$ unset dataarray
c@CW8:~$ declare -A dataarray
c@CW8:~$ dataarray["kill"]="bill";dataarray["epic"]="deal";dataarray["blonde"]="chick";dataarray["linux"]="questions";
c@CW8:~$ echo ${dataarray[*]}
bill chick questions dea
 
1 members found this post helpful.
Old 11-25-2010, 08:38 AM   #15
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Aha! A measly declare?

Thanks for that (I thought declare was optional - in this case, it is definitely not).
 
  


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
Help needed for using awk to parse a file to make array for bash script tallmtt Programming 12 04-14-2012 02:16 PM
Bash + Reading values(numbers) from a file and storing them into an array SuchANewb Linux - Newbie 5 11-04-2010 09:11 AM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 11:07 AM
Awk output to bash array? kj6loh Programming 4 09-07-2009 01:36 PM


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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration