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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read a File line by line and split into array word by word
# 1  
Old 03-31-2014
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 works).



Requirement
file1.txt
================================
The world is a wonderful place to stay
Moon is a satellite of earth
Happiness an fun is all that matters in the world

I have words in a file that require separate handling,as part of my requirement I need to parse the file word by word for each line

example
arr[0]=the
arr[2]=world
.
.
arr[7]=stay

That would be the output of first parse




Thanks ! for your help,its always a fun SmilieSmilie to come to this forum
# 2  
Old 03-31-2014
Code:
tr ' ' '\n' < file

# 3  
Old 03-31-2014
Hello,

Please use the code tags for any command or code as per forum rules.
Following may help you.

Code:
awk '{for(i=1;i<=NF;i++) {print $i}}' file_name

Output will be as follows.

Code:
The
world
is
a
wonderful
place
to
stay
Moon
is
a
satellite
of
earth
Happiness
an
fun
is
all
that
matters
in
the
world


Thanks,
R. Singh
# 4  
Old 03-31-2014
Thanks Singh,
sorry if I have misrepresented my requirement
1)Parse a file line by line
2)on the first parse split the line into word array
3)do some processing[which I can handle myself]
4)reset array
repeat step 1 to 4
# 5  
Old 03-31-2014
Hi guy,
Don't know if u r using bash for this, if so, u could try this:
Code:
$ ./k.sh file3
Line   1: The world is a wonderful place to stay
arr[0]=The
arr[1]=world
arr[2]=is
arr[3]=a
arr[4]=wonderful
arr[5]=place
arr[6]=to
arr[7]=stay
Line   2: Moon is a satellite of earth
arr[0]=Moon
arr[1]=is
arr[2]=a
arr[3]=satellite
arr[4]=of
arr[5]=earth
Line   3: Happiness an fun is all that matters in the world
arr[0]=Happiness
arr[1]=an
arr[2]=fun
arr[3]=is
arr[4]=all
arr[5]=that
arr[6]=matters
arr[7]=in
arr[8]=the
arr[9]=world
$ cat k.sh 
#!/bin/sh
infile=$1
i=1
while read -r line
do
    printf "Line %3d: %s\n" $i "$line"
    arr=($line)
    for j in `seq 1 ${#arr[@]}`
    do
        let j=j-1
        printf "arr[%d]=%s\n" $j "${arr[$j]}"
    done
    let i=i+1
done<$infile

and if not so, you may need to change the code segment that I was using to set the array 'arr''s value.
# 6  
Old 03-31-2014
Hello,

Please let us know the input and expected output for same.
Kindly use code tags while posting the same.

Thanks,
R. Singh
# 7  
Old 03-31-2014
Code:
 IFS=' ' read -a words <<< "$line"
             for element in "${words[@]}"
               do
                echo -e "$element\c" >> "/home/user/scripts/cmgtas/maildynamic.html"
                 echo "$element"
               done
              echo "" >> "/home/user/scripts/cmgtas/maildynamic.html"
        else 
              echo "$line" >> "/home/user/scripts/cmgtas/maildynamic.html"
        fi

IFS=$OLDIFS

Thanks all I have found the solution

Last edited by Scrutinizer; 03-31-2014 at 07:49 AM.. Reason: icode ===> code tags
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 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... (4 Replies)
Discussion started by: ashwin3086
4 Replies

3. 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

4. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

5. 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

6. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

7. UNIX for Dummies Questions & Answers

regular expression for replacing the fist word with a last word in line

I have a File with the below contents File1 I have no prior experience in unix. I have just started to work in unix. My experience in unix is 0. My Total It exp is 3 yrs. I need to replace the first word in each line with the last word for example unix have no prior experience in... (2 Replies)
Discussion started by: kri_swami
2 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. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question