LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 09-21-2014, 09:23 AM   #1
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Rep: Reputation: 135Reputation: 135
associative array in bash


I have a file with lines which I am taking input by $1:

PHP Code:
   X B C D E
    X G H I J
    X L M N 
    Y G  
    Z B
    Y L 
In each line starts with X, the key is the 2nd element and the values are the rest elements.

I am reading the file line by lines creating associate array for each.

Code:
 while read LINE
    do
     INPUT=$(echo $LINE |awk '{print $1}')
     if [[ "$INPUT" = X ]]
     then
         key_name=$(echo $LINE | awk '{print $2}')
         declare -A dependencies 
         value_names=($(echo $LINE|awk '{$1=$2=""; print $0}'))
         dependencies[key_name]=value_names
         echo -e "\nvalues of $key_name are ${key_name[*]}\n"
         sleep 1
     fi
    done < $1
So I am losing the value for each line reading.

But I need to store all the lines with X in the associate arays,
because I need to search for the key later for the later lines, lets say: a line start with Y, and it has `G`, so here I need to find the valuess from the associated arrays
with key G.

Can anyone suggest some idea how to store all lines start with X in a single associative array by reading line line the file? Or any better approach?

Here from the sample input given, the output will be in 3 lines:

PHP Code:
H I J
    C D E
    M N 

Here X,Y,Z cant be keys/values as they are recognizing the lines what to do with the next characters. If X store the rest in KEY-PAIR or if Y or Z extract the values from associative arrays.

Last edited by divyashree; 09-21-2014 at 10:49 AM.
 
Old 09-21-2014, 09:49 AM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,338

Rep: Reputation: Disabled
I'm not sure I understand exactly what you're trying to do, and the script seems to contain a number of errors/mistakes.

You're declaring the variable dependencies as an associate array, but then you put the actual contents in a variable called key_value. Also, you store the data from LINE in value_names, but store something called pkd_depends in key_value (incorrectly referenced as an associative array with a static, probably incorrect index).

If you declare a variable as an associative array with declare -A, you can use any text as an array index. The question is, what do you wish to use as an indiex? One of the (other) fields from the line of the input file?
 
Old 09-21-2014, 10:40 AM   #3
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by Ser Olmy View Post
I'm not sure I understand exactly what you're trying to do, and the script seems to contain a number of errors/mistakes.

You're declaring the variable dependencies as an associate array, but then you put the actual contents in a variable called key_value. Also, you store the data from LINE in value_names, but store something called pkd_depends in key_value (incorrectly referenced as an associative array with a static, probably incorrect index).

If you declare a variable as an associative array with declare -A, you can use any text as an array index. The question is, what do you wish to use as an indiex? One of the (other) fields from the line of the input file?
Thank you Ser, for responding. I have updated some sample output and removed the typos.
 
Old 09-21-2014, 11:19 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
If you are going to use bash, then use it, no need to go off to other commands like awk.

Are you looking for something like:
Code:
#!/usr/bin/env bash

declare -A deps

while read -r id index rest
do
	[[ "$id" == X ]] && deps[$index]="$rest"
done<"$1"

for i in "${!deps[@]}"
do
	echo "deps index $i has values: ${deps[$i]}"
done
 
Old 09-22-2014, 12:13 PM   #5
DarrenDrapkin
Member
 
Registered: Aug 2014
Location: Leeds, England
Distribution: Slackware x86 64 version 15.0
Posts: 127
Blog Entries: 3

Rep: Reputation: 27
To me finding the symbol Y after a symbol X is quite easy in awk

/X/{Y = $2 ;print Y;}

now using the value of y to tag the rest of the line is a bit harder
Z=($3 to $nf)
unfortunatelly this second expression does not work; or if it does, awk has a new feature
 
  


Reply



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
Combine two Bash arrays into a new associative array bits45 Programming 7 08-26-2014 01:27 PM
[SOLVED] AWK associative array problem rng Linux - General 17 02-12-2014 11:48 PM
[SOLVED] Associative array in bash not unsetting grail Programming 4 04-07-2011 09:11 AM
create a global associative array variable inside a function of a bash script konsolebox Programming 3 07-14-2009 06:08 AM
Sorting through associative array with user input nobody123 Linux - Newbie 3 04-08-2009 01:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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
Open Source Consulting | Domain Registration