Reading a file into array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading a file into array
# 1  
Old 07-19-2013
Reading a file into array

Hi,

I need to read a file into array and print them in a loop:-
1st file :-cat a.txt
RC1
RC2
RC3
RC4

My Program:-
Code:
#!/bin/ksh
index=0
while [ $index -lt 5 ]
do
       read cnt[index]<a.txt
       print "cnt [$index] value is ${cnt[index]}
       index=`expr $index + 1`
done

Moderator's Comments:
Mod Comment Code tags for code, please.

Result expected :-
Code:
cnt[0] value is RC1
cnt[1] value is RC2
cnt[2] value is RC3
cnt[3] value is RC4

But i am getting the below result :-
Code:
cnt[0] value is RC1
cnt[1] value is RC1
cnt[2] value is RC1
cnt[3] value is RC1

I think there is a problem the way i am reading the file into the array

Last edited by Scott; 07-20-2013 at 01:00 AM.. Reason: Code tags
# 2  
Old 07-19-2013
<a.txt will reopen the file every single time, and give you the same line every time. What you want is more like

Code:
while read ARR[index]
do
        let index=index+1
done < filename

But first I must ask, why are you reading a whole file into an array? Array sizes are not unlimited, and there's usually no need to do so anyway.
# 3  
Old 07-19-2013
Try also
Code:
cnt=($(tr '\n' ' ' <file))
echo ${#cnt[@]}
4
echo ${cnt[@]}
RC1 RC2 RC3 RC4

# 4  
Old 07-19-2013
Thanks corona688

i have this code and it is working fine.
Actual issue is to read input from 2 files by line wise and print them as below:-
Code:
>cat a.txt 
RC1
RC2
RC3
RC4
>cat b.txt
11
22
33
44

Desired output :-
Code:
RC1 - 11
RC2 - 22
RC3 - 33
RC4 - 44

If i am using passing of file method,i could not arrive that the desired output .so i am trying other ways .Can you help me if it can be achieved in another method.

Thanks again !!

Last edited by Scott; 07-20-2013 at 01:00 AM.. Reason: Code tags
# 5  
Old 07-19-2013
Lots of ways, actually, none of which mean slurping entire whole files into memory(usually a mistake).

Code:
paste -d "-" file1 file2

Code:
exec 5<file1 # Open into FD 5
exec 6<file2 # Open into FD 6

while read A<&5 && read b<&6 # Read from FD 5 and 6
do
        echo "$A - $B"
done

exec 5<&- # Close FD 5
exec 6<&- # Close FD 6

# 6  
Old 07-19-2013
Thanks Corona688 !!
You made it so simple
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

2. Shell Programming and Scripting

Reading a file into an array

Hi I have a file with contents as below : server | ABC Issue : File System Missing XYZ Issue : Wrong Syntax PQR Issue : Old File to be removed Now I am looking for an o/p similar to server <tab> ABC Issue : File System Missing <tab> XYZ Issue : Wrong Syntax <tab>... (4 Replies)
Discussion started by: deo_kaustubh
4 Replies

3. Shell Programming and Scripting

Reading columns from a text file and to make an array for each column

Hi, I am not so familiar with bash scripting and would appreciate your help here. I have a text file 'input.txt' like this: 2 3 4 5 6 7 8 9 10 I want to store each column in an array like this a ={2 5 8}, b={3 6 9}, c={4 7 10} so that i can access any element, e.g b=6 for the later use. (1 Reply)
Discussion started by: Asif Siddique
1 Replies

4. Shell Programming and Scripting

Help with reading column in array

I have some version problem to use this code in my server while read line; do read -A array <<<$line <---------- the server dont read <<< n=${#array} for ((i=1;i<$n;i++)); do print "${array}" done func=${array} data1=${array} data2=${array} ... (1 Reply)
Discussion started by: gavin_L
1 Replies

5. Shell Programming and Scripting

reading data from a file to an array

I need some help with this code below, i doesnt know why it will run twice with my function, but my function only got if else, any other way that can read line and put into array? while read line; do read -A array <<<$line n=${#array} for ((i=1;i<$n;i++)); do print... (1 Reply)
Discussion started by: gavin_L
1 Replies

6. Shell Programming and Scripting

Reading from a file and assigning to an array in perl

I wrote a simply perl that searched a file for a particualr value and if it found it, rite it and the next three lines to a file. Now I have been asked to check those next three lines for a different value and only write those lines if it finds the second value. I was thinking the best way to... (1 Reply)
Discussion started by: billprice13
1 Replies

7. Programming

Reading Scientific notation from file and storing in array

Hi, I am trying to read a set of numbers that are in scientific notation into a file so I can do some math on them, but when I display the array contents the numbers aren't the same as the numbers in the file. Could someone explain why? Thanks. int main() { double fArray; ... (3 Replies)
Discussion started by: Filter500
3 Replies

8. Shell Programming and Scripting

Reading from array

Hi, I have an array like below... Table="table1" Table="table2" Table="table3" Table="table4" ..... Table="tablen" I want to retireve the values from the array and need to pass this to a db2 command to create a view like below. the number of values in the array will vary ... (1 Reply)
Discussion started by: ratheeshjulk
1 Replies

9. Shell Programming and Scripting

Bash: Reading out rows of a file into a dynamic array and check first literal

Hello, i have a file "Movie.ini" looking e.g. like follows * MOVIE A bla bla MOVIE B blubb blubb MOVIE C I'd like to read the file "Movie.ini" with cat and grep and check whether it includes the string MOVIE only with a '*' at the beginnig. By doing "cat Movie.ini| grep MOVIE... (14 Replies)
Discussion started by: ABE2202
14 Replies

10. UNIX for Dummies Questions & Answers

Reading a file into an array

I have a file that is a text file, how to get all the words into and array, i am able to get each line but not each word :(. Here is what i searched and already found...https://www.unix.com/shell-programming-scripting/99207-pipe-text-file-into-array.html. This one reads a whole line into... (6 Replies)
Discussion started by: SasankaBITS
6 Replies
Login or Register to Ask a Question