Most reliable way to store file contents in an array in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Most reliable way to store file contents in an array in bash
# 1  
Old 02-08-2010
Most reliable way to store file contents in an array in bash

Hi Guys,

I have a file which has numbers in it separated by newlines as follows:

Code:
1.113
1.456
0.556
0.021
-0.541
-0.444

I am using the following code to store these in an array in bash:

Code:
FILE14=data.txt
ARRAY14=(`awk '{print}' $FILE14`)

However this is causing an indexing problem (awk starts with index 1 and bash with index 0)

What is the best way to store these numbers from the file in an array? (I would prefer not using Awk)

Thanks.
# 2  
Old 02-08-2010
Code:
FILE14=data.txt
ARRAY14=( $(cat $FILE14 | tr '\n' ' ') )
# testing 
for i in $(seq 0 $((${#ARRAY14[@]}-1)))
do
    echo "i=$i - ${ARRAY14[$i]}"
done

# 3  
Old 02-08-2010
Or (bash):
Code:
FILE14=data.txt
ARRAY14=( $(<"$FILE14") )
for (( i=0; i<${#ARRAY14[@]}; i++ )); do
  echo ${ARRAY14[i]}
done

Enumerating alternative:
Code:
for i in "${ARRAY14[@]}" ; do
  echo $i
done

# 4  
Old 02-08-2010
Thanks, Scritinizer. I forgot to mention, the original file also has blank lines in it which I want to store in the array as well. How can I modify the script so that I can store the blank values in the array?
# 5  
Old 02-08-2010
In that case I think you would need something like this:
Code:
FILE14=data.txt
i=0
while read line; do
  ARRAY14[$i]=$line
  i=$((i+1))
done < "$FILE14"
for (( i=0; i<${#ARRAY14[@]}; i++ )); do
  echo ${ARRAY14[i]}
done

# 6  
Old 02-08-2010
Ok thanks that worked.
# 7  
Old 02-08-2010
You can use mapfile (bash 4.x only).
Code:
# for i in {a..z};do echo $i >> infile;done
# unset array

# mapfile -O 1 array < infile
# echo ${array[3]}
c

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

C TCP/IP Reliable Transmission project not reliable

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: We must do the following for a massive coding project that is due at 12:20PM on Monday, July 22, 2013. We are to... (1 Reply)
Discussion started by: kowit010
1 Replies

2. Shell Programming and Scripting

Write array contents to file

Hi, I have a bash script that currently holds some data. I am trying to write all the contents to a file called temp.txt. I am using echo ${array} > temp.txt The problem that I am experiencing is that the elements are being written horizontally in the file. I want them written... (5 Replies)
Discussion started by: Filter500
5 Replies

3. Shell Programming and Scripting

Read the contents of a file and store them in a variable

Hi Gurus, I am trying for a scenario where in I want to read the contents of a file line by line and then store them in variables. Below is the script: #!/bin/ksh while read line do id=`echo $line | cut -f1 -d |` name=`echo $line | cut -f2 -d |` echo $id ... (11 Replies)
Discussion started by: svajhala
11 Replies

4. Shell Programming and Scripting

Store table contents in csv file

I need to write a script to store the contents of a table in a csv file I'm using Toad, it's a Oracle database. (5 Replies)
Discussion started by: ladyAnne
5 Replies

5. Shell Programming and Scripting

bash Script: Issue with iterating Directory and store into array

Hi all, I am working on a backup based script, in which it enters to a directory and check the sub-directories and copy the names into an array. cd $CPFs k=0 for i in * do if then ARRs="$i" k=$(($k+1)) #echo "$i" ... (19 Replies)
Discussion started by: canishk
19 Replies

6. Shell Programming and Scripting

How to store contents of a command in array of variables in shell script?

I want to store contents of command dir in array of variables For eg: dir contents are command d2 demovi~ file inven java new untitled folder d1 demovi er1 filename inven~ myfiles ubuntu desktop xmms ----------------------------------- I... (3 Replies)
Discussion started by: netresearch
3 Replies

7. Shell Programming and Scripting

Prase a file and store and result to an array

Dear all, I have a file having the following formats: ThreadFail=Web1=1234 ThreadFail=Web2=2345 ThreadFail=Web3=12 ConnectionFail=DB1=11 ConnectionFail=DB2=22 The number of lines will be different from every time . How can I parse the file and store the result to an a array inside... (6 Replies)
Discussion started by: youareapkman
6 Replies

8. UNIX for Dummies Questions & Answers

compare array contents with file

I have an array "arrA" with the following contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf My data file "Files.dat" has the same contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf I have a script that compares... (0 Replies)
Discussion started by: orahi001
0 Replies

9. UNIX for Dummies Questions & Answers

How to store the values in a file into an array

Hi, I do have a file and the contents are as follws: 10 20 30 40 50 Now I want to store those values into an array. How can be done this ?? (3 Replies)
Discussion started by: risshanth
3 Replies

10. Shell Programming and Scripting

Can I assign the contents of file into an array?

I am trying to assign the contents of file e.g ls "$HOME" into an array. If it is possible then please guide me without using the concept of awk,sed, and perl16 Thanks (10 Replies)
Discussion started by: murtaza
10 Replies
Login or Register to Ask a Question