LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 12-09-2014, 12:58 PM   #1
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Rep: Reputation: Disabled
Bash assigning array in loop


input.txt
Code:
x1=1658
x12=1658
x21=9795
x5=18322
x77=3265
x68=6001
x30=123
x3=8091
x4=636739
Code:
for (( i = 1; i <= $n; i++ ))
	do
	echo 'Pick from above list'
	read value
	arr_val=($(sed 's/=/ /g' input.txt | grep -w "$value" | awk '{print $2}'))
       done

echo ${arr_val[@]}
So the user would input the number of values they want as $n then which values they want x21 and x38 for example.
Using Bash all I want to to do is create an array from the output above but when i use echo to check, it only seems to be outputting the last value

Ive tried setting arr_val[$i]=($(sed........)
But that is giving me an error


Does anyone have any advice?

Thanks
 
Old 12-10-2014, 02:50 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
sorry, I do not really understand what do you need.
 
1 members found this post helpful.
Old 12-10-2014, 05:37 AM   #3
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Rep: Reputation: Disabled
Your program looks complicated... try this.


#!/bin/bash

i=0
while read LINE
do
arr1[$i]=`echo $LINE | cut -d"=" -f2`
i=`expr $i + 1`
done < input.txt
 
1 members found this post helpful.
Old 12-10-2014, 06:04 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
I am with pan64 in that from your explanation and your example code it is difficult to follow what you are trying to do?

As far as your array goes, every time your loop hits arr_val=() it is resetting the values in the array to the latest read, hence
after you finish the loop you will only have then values from the last read
 
1 members found this post helpful.
Old 12-10-2014, 10:07 AM   #5
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
I am with pan64 in that from your explanation and your example code it is difficult to follow what you are trying to do?

As far as your array goes, every time your loop hits arr_val=() it is resetting the values in the array to the latest read, hence
after you finish the loop you will only have then values from the last read
Sorry, Ill try to explain it better but essentially i just dont want it to be resetting the values. I want that each time it goes through the loop it is outputting to a different row of the array. So I guess more like how I have it below for assigning the array (though this gives me an error).

Code:
read n

for (( i = 1; i <= $n; i++ ))
	do
	echo 'Pick from above list'
	read value
	arr_val[$i]=($(sed 's/=/ /g' input.txt | grep -w "$value" | awk '{print $2}'))
       done

echo ${arr_val[@]}
So say the user says n=4.
Then they would input there 4 choices. For example:
x12
x5
x68
x30

The desired array output would be:
1658
18322
6001
123


Maybe Im going about it all wrong, but I hope that clears up what I am trying to get at

Thanks
 
Old 12-10-2014, 10:36 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
so you want to get a number <n>, and read <n> lines from that input file. All the <n> inputs will be asked, as column 1 and column 2 will be put into your array.
Code:
for (( i = 1; i <= $n; i++ ))
do
	echo 'Pick from above list'
	read value
        arr_val[$i]=$(awk -F= -v var="$value" '$1==var{print $2}' input.txt)
done
probably works, but still not really sure. Also there is no error handling here, what will happen if user entered an illegal value?
 
1 members found this post helpful.
Old 12-10-2014, 11:11 AM   #7
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
so you want to get a number <n>, and read <n> lines from that input file. All the <n> inputs will be asked, as column 1 and column 2 will be put into your array.
Code:
for (( i = 1; i <= $n; i++ ))
do
	echo 'Pick from above list'
	read value
        arr_val[$i]=$(awk -F= -v var="$value" '$1==var{print $2}' input.txt)
done
probably works, but still not really sure. Also there is no error handling here, what will happen if user entered an illegal value?
Awesome! That worked, though I have no idea why it did versus what I had. I was under the impression my job was not working because it didnt like the
Code:
arr_val[$i]=
As for errors, seems that if they put in something that doesn't exist for value it just write out $value instead which seems ok as it would pretty noticeable

Thanks Again!
 
Old 12-10-2014, 11:17 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
you made an extra ( ) around that expression.
Code:
v=$(awk -F= -v var="$value" '$1==var{print $2}' input.txt)
if [ -z "$v" ] # here comes your error handling
else
arr_val[$i]="$v"
 
1 members found this post helpful.
Old 12-10-2014, 07:27 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Assuming using bash v4+, you could use associative arrays and keep it all in bash:
Code:
declare -A input_array

while IFS== read -r index value
do
  input_array[$index]="$value"
done<input.txt

for (( i = 0; i < n; i++ ))
do
  echo 'Pick from above list'
  read value
  
  arr_val+=( ${input_array[$value]:-$value does not exist} )
done
I leave you to set 'n' and I assume at some point you do actually display the 'above list' (tip: you could use the while loop above to display it)
 
  


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
[SOLVED] Assigning Multiple Comma Separated IP's To A Bash Array metallica1973 Programming 3 06-29-2012 04:30 AM
[SOLVED] bash loop or array atmos1234 Programming 6 05-22-2012 10:47 AM
Bash for loop use variable name as an array fur Programming 4 11-03-2011 02:51 PM
Assigning variables in a bash for loop JDska55 Linux - Newbie 6 06-18-2009 03:37 PM
For loop in bash using ls to get array of file names bioinformatics_guy Linux - Newbie 5 02-19-2009 10:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:22 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