LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-02-2013, 10:28 AM   #1
eamesj
Member
 
Registered: May 2006
Posts: 51

Rep: Reputation: 1
bash split array and assign a variable to an element


Hi,

wonder if you can help. I have a bash script which is generating an array...

Quote:
echo "Enter the list, type 'quit' to exit"
while read -r input ; do
[[ $input == quit ]] && break
array+=( $input )
done
echo ${array[@]}
which when the input is
1 a z
2 b y
3 c x
quit

gives the array three elements. Is there a way i can split these elements based on column and assign them to a variable?

so if
column1=$number (1, 2 and 3)
column2=$letter (a, b and c)
column3=$anotherletter (z, y and x)

i can perform an action on each e.g.

Quote:
for index in ${!array[*]} ; do
echo "$array[1] has value ${number}, letter ${letter}"
done
Thanks,
 
Old 03-02-2013, 11:41 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

Your best bet is simply to use another read command, either with separate variables or another array.

Code:
for i in "${!array[@]}"; do
    read -r number letter anotherletter <<<"${array[i]}"
    echo "$array[i] has value ${number}, letter ${letter}, and letter $anotherletter"
done

string manipulations in bash
 
Old 03-02-2013, 11:56 AM   #3
eamesj
Member
 
Registered: May 2006
Posts: 51

Original Poster
Rep: Reputation: 1
awesome david, works well. thanks
 
Old 03-02-2013, 12:19 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Great. Please mark the thread as solved.

Oh, and I almost forgot, be sure to always quote your variable expansions, otherwise shell word-splitting will take place (with a few minor exceptions, such as inside [[..]]).
Code:
while read -r input ; do
    [[ $input == quit ]] && break
    array+=( "$input" )
done
echo "${array[@]}"
Without the quotes, the initial $input value will be split into multiple array elements, and the final echo command will have all entries printed with only single spaces between them, regardless of their original formatting.

It's vital in scripting to understand how the shell handles arguments and whitespace. Learn how to do it right!
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
 
  


Reply

Tags
array, bash, element


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
Why Perl's foreach loop can't use an array element as the control variable? password636 Programming 1 07-22-2011 09:58 AM
get index of an element in array in bash mangatmodi Programming 4 11-20-2009 07:45 AM
how to assign a u_short variable to a u_char array ? aryan1 Programming 1 07-29-2009 06:15 AM
How do you assign an html element as a php variable? 3vra Programming 1 02-06-2009 05:51 PM


All times are GMT -5. The time now is 03:59 AM.

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