Reading a file into array | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reading a file into array

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-19-2013
satishmallidi satishmallidi is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 29 December 2013, 7:07 AM EST
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
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:
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
Sponsored Links
    #2  
Old 07-19-2013
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 November 2014, 10:03 AM EST
Location: Saskatchewan
Posts: 19,905
Thanks: 845
Thanked 3,413 Times in 3,198 Posts
<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.
Sponsored Links
    #3  
Old 07-19-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 22 November 2014, 8:25 AM EST
Location: Aachen, Germany
Posts: 4,678
Thanks: 78
Thanked 1,154 Times in 1,084 Posts
Try also
Code:
cnt=($(tr '\n' ' ' <file))
echo ${#cnt[@]}
4
echo ${cnt[@]}
RC1 RC2 RC3 RC4

    #4  
Old 07-19-2013
satishmallidi satishmallidi is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 29 December 2013, 7:07 AM EST
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
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
Sponsored Links
    #5  
Old 07-19-2013
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 November 2014, 10:03 AM EST
Location: Saskatchewan
Posts: 19,905
Thanks: 845
Thanked 3,413 Times in 3,198 Posts
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

Sponsored Links
    #6  
Old 07-19-2013
satishmallidi satishmallidi is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 29 December 2013, 7:07 AM EST
Posts: 21
Thanks: 0
Thanked 1 Time in 1 Post
Thanks Corona688 !!
You made it so simple
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading columns from a text file and to make an array for each column Asif Siddique Shell Programming and Scripting 1 04-06-2013 05:08 PM
reading data from a file to an array gavin_L Shell Programming and Scripting 1 09-19-2012 02:11 AM
Reading from a file and assigning to an array in perl billprice13 Shell Programming and Scripting 1 05-27-2012 12:46 PM
Reading Scientific notation from file and storing in array Filter500 Programming 3 12-16-2011 03:45 PM
Reading a file into an array SasankaBITS UNIX for Dummies Questions & Answers 6 07-22-2009 05:17 AM



All times are GMT -4. The time now is 11:22 AM.