Read line by line and store a word in array

Hi,

I would like to read a file line by line and then store the whole line word by word in array so that I can do specific manipulation on each word.

 
 
$ cat test.txt
hello how are you
i am good

I would like to have an array created dynamically so that first time , the array will have 4 values (hello,how,are,you) and then change hello to hi and store it in the array again .Next it will have 3 values (i,am,good) and prints them.The file can have any number of lines (<1000) and each line can have upto (300 words)

 
cat newfile.txt
hi how are you
i am good
i=0;
for j in `cat test.txt` 
do
   array[$i]=$j; 
   #do manipulations here
    i=$(($i+1)); 
   
done 
echo "Value of third element in my array : ${array[3]} ";

or if you want to read it line by line

while read line
do
  array=($line)
  #do manipulations here
  echo "Value of third element in my array : ${array[3]} "
done < test.txt

Do you have one array and read it, manipulate it, print it & reuse it, or do you want your entire file in array elements? And, array manipulation differs from shell to shell, so what shell do you use?

I would like to store each record in the array word by word. Ideally I want to loop through the file and store each word of the record in array and manipulate and print it again in record by record ((ie one array per record and as I am looping through I can do the manipulation ,print the new record and use the same array for next record) . After doing the manipulation , may be we can use a new array to store the updated record information and print it. In this case there will be 2 arrays and for each record it can be reused. Hope I haven't confused you.

And I am using Korn Shell.

pseudo code (my idea)

while read line 
do

store record one word by word in array
loop though the array
read each word and do the manipulation
store the manipulated record in new array word by word
end the loop
print the new array

done < file.txt


The korn shell offers an "array read". man ksh :