LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-02-2010, 04:34 PM   #1
ghantauke
Member
 
Registered: Nov 2010
Posts: 114

Rep: Reputation: 6
Bash: Reading file into array


Assume I have a file named file.txt with the following contents
Code:
19 man
24 house
44 dyam
90 random
I want to read the file into array and store each line in each index. I've tried using the following code.
Code:
dataarray=($( < file.txt ))
It stores each word in each index rather than each line in each index.
Can someone please help me.
 
Old 12-02-2010, 06:37 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,877

Rep: Reputation: 350Reputation: 350Reputation: 350Reputation: 350
www.google.com/linux is your friend for anything GNU/Linux/Unix. I entered search terms "bash array input" and found this: http://www.linuxconfig.org/Bash_scripting_Tutorial. Scroll down to "8.2. Read file into bash array".
 
Old 12-02-2010, 07:00 PM   #3
kakaka
Member
 
Registered: Sep 2003
Posts: 382

Rep: Reputation: 87
Naturally it is much less elegant than what you started with,
but you could always force bash to do what you want like this:


Code:
# Just to be formal, declare the array.
declare -a data_array



# Force reading of each line into one element of the array.

line_cnt=0

while :
    do

        read data_line

        if [ $? -ne 0 ]
            then
                break
        fi

        data_array[$line_cnt]="$data_line"

        (( line_cnt++ ))

    done  <  file.txt




# To verify one line per element, display the contents of one element per line.

line_num=0

while [  $line_num  -lt  $line_cnt  ]
    do

        echo "data_array[$line_num]=${data_array[$line_num]}"

        (( line_num++ ))

done
 
Old 12-02-2010, 11:06 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
Well I would think the simple loop would be fine:
Code:
while read -r line; do arr+=( "$line" ); done<file
 
  


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
[SOLVED] reading unix file permissions into a bash array for processing dazdaz Programming 11 12-06-2010 04:21 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 - Reading csv delimited file to array and for further manipulation BLWEGRZYN Programming 1 01-06-2010 10:38 PM
BASH: Reading long filenames into an array using a loop DaneM Programming 12 09-11-2009 08:24 AM
Reading crontab into bash array nutthick Programming 2 05-22-2006 01:44 PM


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