Reading a file into an array | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Reading a file into an array

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-21-2009
SasankaBITS SasankaBITS is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 17 August 2010, 11:29 PM EDT
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
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...http://www.unix.com/shell-programmin...nto-array.html.

This one reads a whole line into variable filename[*], i need each word to be in filename[*], How do i do this.
Thank you in advance
Sponsored Links
    #2  
Old 07-21-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 2 September 2014, 12:23 PM EDT
Location: St. Gallen, Switzerland
Posts: 6,227
Thanks: 121
Thanked 451 Times in 411 Posts
Example in ksh:

Code:
# cat infile
most people
just do not
use code tags
# set -A myarray `tr -s '\n' ' ' < infile`
# echo ${myarray[*]}
most people just do not use code tags

Sponsored Links
    #3  
Old 07-21-2009
SasankaBITS SasankaBITS is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 17 August 2010, 11:29 PM EDT
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks, that was simple and effective.
Here is another problem that has been bugging me, have a look at this too if u can. . Thank you.
---------- Post updated at 06:44 PM ---------- Previous update was at 06:34 PM ----------

Take this code as example i have been trying to find out why my code was not working and this was happening,
CODE:


Code:
string="pop the one"
set -A myarray $string
echo ${myarray[0]}
echo ${myarray[1]}
echo ${myarray[2]}

OUTPUT
pop
the
one

This output comes up correctly when i just type these commands.

But, when i write the whole code in a file and save it as mycode and then run it as ./mycode it says:
./mycode: line 2: set: -A: invalid option
set: usage: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]


Why is this so? can u explain?

Last edited by SasankaBITS; 07-21-2009 at 09:27 AM.. Reason: code tags, PLEASE!
    #4  
Old 07-21-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 2 September 2014, 11:55 PM EDT
Location: Foxborough, MA
Posts: 7,644
Thanks: 150
Thanked 577 Times in 542 Posts
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
Sponsored Links
    #5  
Old 07-21-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 2 September 2014, 12:23 PM EDT
Location: St. Gallen, Switzerland
Posts: 6,227
Thanks: 121
Thanked 451 Times in 411 Posts
Hm, make sure using a shebang in the head like

Code:
#!/usr/bin/ksh
...

for example.
Sponsored Links
    #6  
Old 07-22-2009
SasankaBITS SasankaBITS is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 17 August 2010, 11:29 PM EDT
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by zaxxon View Post
Hm, make sure using a shebang in the head like

Code:
#!/usr/bin/ksh
...

for example.
wow, that worked. Thanks. If its not too much trouble can u explain why?
Sponsored Links
    #7  
Old 07-22-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 2 September 2014, 12:23 PM EDT
Location: St. Gallen, Switzerland
Posts: 6,227
Thanks: 121
Thanked 451 Times in 411 Posts
Executing your script without shebang will use a default shell I guess or the one that you current use. With the shebang you tell the system explicit which shell to use to execute that script. Since I wrote the array how it is written in ksh, it would be good to tell the system to explicit use ksh by the shebang.
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
awk: reading into an array and then print the value corresponding to index akshaykr2 Shell Programming and Scripting 19 06-24-2009 06:01 PM
awk reading many fields to array pinnacle Shell Programming and Scripting 2 04-22-2009 08:07 PM
Reading Of Array Element in Unix ravi_rn Shell Programming and Scripting 2 12-30-2008 01:48 PM
Reading from while loop into an array ssuresh1999 UNIX for Dummies Questions & Answers 2 08-12-2008 11:53 AM
Reading data into muti-dimentional array - in perl zap Shell Programming and Scripting 5 05-04-2004 10:15 AM



All times are GMT -4. The time now is 12:36 AM.