Read line by line and store a word in array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read line by line and store a word in array
# 1  
Old 08-06-2013
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.


HTML Code:
 
$ 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)


Code:
 
cat newfile.txt
hi how are you
i am good


Last edited by ashwin3086; 08-06-2013 at 06:03 AM..
# 2  
Old 08-06-2013
Code:
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

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


Last edited by Arun_Linux; 08-06-2013 at 06:19 AM..
# 3  
Old 08-06-2013
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?
# 4  
Old 08-06-2013
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.

Code:
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


Last edited by ashwin3086; 08-06-2013 at 12:15 PM..
# 5  
Old 08-06-2013
The korn shell offers an "array read". man ksh:
Quote:
read [-Aprs] [-d delim] [ -n n] [[ -N n] [[-t timeout] [-u unit] [vname?prompt] [ vname ... ] The shell input mechanism. One line is read and is broken up into fields using the characters in IFS as separators . . .
The -A option causes the variable vname to be unset and each field that is read to be stored in successive elements of the indexed array vname.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

2. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

3. Shell Programming and Scripting

Read line of files and call store procedures

Hi, I wish to write a piece of code to read lines of the flat file and cut each lines of record to a variable, then use this variable to pass as an parameter of a Sybase stored procedures, if there are 5 lines of record from the text file, then this stored procedures will be called 5 times.... (8 Replies)
Discussion started by: newbie2011
8 Replies

4. Shell Programming and Scripting

Read last word of the line.

Hi, I need a script to read last word of the line and out put in some temp file. i it can contain any word like: My name is Harry. or My zip code is 24490 or it can be My secret code is 024H I just need last word of the line (Harry, or 2440 or 024H) Thanks for the posts below.... (10 Replies)
Discussion started by: HarryReid
10 Replies

5. Shell Programming and Scripting

How to read a file line by line and store it in a variable to execute a program ?

Hello, I am quite new in shell scripting and I would like to write a little scritp to run a program on some parameters files. all my parameters files are in the same directory, so pick them up with ls *.para >>dirafter that I have a dir file like that: param1.para param2.para etc... I... (2 Replies)
Discussion started by: shadok
2 Replies

6. Shell Programming and Scripting

Read last word of nth line

Hi people; i want to read the last word of the 14th line of my file1.txt. Here is the EXACT 14th line of the file. 250 SectorPortnum=3,AuxPortInUngo=2,PortDeviceGroup=1,PortDeviceSet=1,PorDevice=1 20 >>> Set. i have to get the word Set. how can i call it and also how... (3 Replies)
Discussion started by: gc_sw
3 Replies

7. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

8. UNIX for Dummies Questions & Answers

query on how to search for a line and read 4th word from that line

Assume I have a text file as below: me con pi ind ken pras ur me con rome ind kent pras urs pintu con mys ind pan pras ki con kit ind sys My requirement, I need to search for "con rome" and if exists, then print 4th word from rome, i.e in above example, since "con rome"... (4 Replies)
Discussion started by: jaggesh
4 Replies

9. Shell Programming and Scripting

Read line by line not word by word

i have this line my,name,is,john stored in a file d.sh and i wish to print the line as string..but im getting word by word...have tried this......... for in $(cat $d.sh); do echo $i done but this is giving me my name is john (6 Replies)
Discussion started by: vadharah
6 Replies

10. UNIX for Dummies Questions & Answers

Read and store each line of a file to a variable

Hi all, I'm quite new to unix and hope that someone can help me on this. I'm using csh. Below is what i intend to do. 1. I stored some data in a file. 2. I intend to read the file line by line and store each line of data into a variable, so that i can used it later. Anyone have any... (4 Replies)
Discussion started by: seijihiko
4 Replies
Login or Register to Ask a Question