LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-03-2004, 09:53 AM   #1
mispunt
LQ Newbie
 
Registered: Apr 2004
Distribution: Mandrake
Posts: 14

Rep: Reputation: 0
[bash] Put words from file to array


Hi am working on a project where I should test two files. if there are some words exactly the same the script should report that.
The documents are normal text documents (or later word documents). I have a for loop to put the words into an array, so I could test two words after I have put the documents into the array. My for loop looks like this:
Code:
for line in `cat $file1 | tr ' ' '\n'`; do
                line1[$counter1]=$line
                counter1=$(($counter1+1))
done
I use this twice, so I should have two arrays. but the problem is that the array is empty after the loop. and when I echo line1[$counter] I see []

Could you tell me what I am doing wrong? I am staring to this problem to long (only one hour) but I can't find a sollution
 
Old 11-03-2004, 10:12 AM   #2
secesh
Senior Member
 
Registered: Sep 2004
Location: Savannah, GA
Distribution: Ubuntu, Gentoo, Mythbuntu, ClarkConnect
Posts: 1,154

Rep: Reputation: 47
Code:
#!/bin/bash
# file: reader
file1=reader

for word in `cat $file1 | tr ' ' '\n'`; do
#       echo $word
        word_array[$counter1]=$word
        counter1=$counter1+1
done


for index in 0 1 2 3 4 5    # Six 'words'
do
  echo ${word_array[index]}
done
--notice the curly-brackets!
 
Old 11-03-2004, 12:04 PM   #3
mispunt
LQ Newbie
 
Registered: Apr 2004
Distribution: Mandrake
Posts: 14

Original Poster
Rep: Reputation: 0
thanks, it works (although your counter didn't work for me)
 
Old 11-04-2004, 04:41 AM   #4
mispunt
LQ Newbie
 
Registered: Apr 2004
Distribution: Mandrake
Posts: 14

Original Poster
Rep: Reputation: 0
On my linux server at home it worked. it was a new version of bash.
Now I am working on the server where the script should be running. It has Red Hat 6.
And the script is not working anymore
one of the much error messages:
Code:
./copy_detector.cgi: word_array[588]=color): command not found
Are arrays only for newer versions of bash? or is there still something wrong?
 
Old 11-04-2004, 10:53 AM   #5
secesh
Senior Member
 
Registered: Sep 2004
Location: Savannah, GA
Distribution: Ubuntu, Gentoo, Mythbuntu, ClarkConnect
Posts: 1,154

Rep: Reputation: 47
i doubt that... here's all i can readily reference for you...

http://www.gnu.org/software/bash/man...ref.html#SEC80
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: put output from `ls` into an array samel_tvom Programming 18 07-31-2005 12:55 AM
Search and Replace: Asian Words to English Words ieeestd802 Linux - Software 0 10-27-2004 07:48 PM
BASH: First words in a line JordanH Programming 7 10-24-2004 10:00 AM
Giving multiple words as argument for bash ivanatora Linux - General 5 01-07-2004 02:04 AM
array in bin/bash how to x2000koh Programming 12 05-09-2003 04:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration