Advertisement

egrep question (i think)

Started by March 08, 2004 09:44 PM
7 comments, last by Mulligan 20 years, 8 months ago
I have a file that lists one word per line (i.e) file.txt----- word1 word2 word3 etc... ------------- and i want to gra all the words and put them into an array. How can i do this?
heh. with what language?

if you want to put them into an array, you''re using some language...
Advertisement
quote: Original post by C-Junkie
heh. with what language?

if you want to put them into an array, you''re using some language...


heh, oh right...

im making a bash script in linux
From here it seems like you can declare an array as:
arrayZ=( one two three four five five )  


So I think you should try
arrayZ=(`cat theFile`)  


You might have to convert the newlines in theFile to spaces to get that to work, probably doable on-the-fly.

Read that page. The rest of that Guide will likely help you too.

[edited by - Doc on March 8, 2004 11:44:35 PM]
My stuff.Shameless promotion: FreePop: The GPL god-sim.
sweet, thanks
Simple.

ARRAY=`egrep "word1|word2|word3" file.txt`;for ITEM in $ARRAY; do echo Hello, $ITEM; done

Try that and experiment with it.
Rate me up.
Advertisement
Yeah, that for loop notation can be a handy way of diong things, too.
My stuff.Shameless promotion: FreePop: The GPL god-sim.
Well... the above scriplet uses egrep to only use sertain words in a file, if you dont need to search for words then Doc''s method looks like the one you need.
Rate me up.
It depends what you want to do with it. It might be easier to load the whole thing into a variable and iterate over each word with that for loop idea. But if you really need an array try the way I suggested.
My stuff.Shameless promotion: FreePop: The GPL god-sim.

This topic is closed to new replies.

Advertisement