LinuxQuestions.org
Register a domain and help support LQ
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-30-2005, 05:16 AM   #1
nkoplm
Member
 
Registered: May 2005
Distribution: Fedora
Posts: 92

Rep: Reputation: 15
bash script help (arrays and strings from files)


i have been looking all ove trying to figgure this one out.


I have a plain text file "in.txt"

it is a new line delimitted text file filled with strings.

how can i get these strings into an array (each line of the file as a different element in the array)?

eventually i want to print a random line from the file to the screen

ex pseudo code

while true
(
echo "a random line read from my text file"
sleep 5s
)



any help?
 
Old 11-30-2005, 09:05 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
bash only does one dimensional arrays. To get really sophisticated with arrays you're best off using Perl.

Having said that you could do an array with the declare built in.

Example:

Say you have a text file named "test" with the following two lines:

Code:
the quick brown fox jumped over the lazy dog
now is the time for all good men to come to the aid of their country
You could create the following simple script:

Code:
#!/bin/bash
declare -a arrtest
arrtest=(`cat test`)
echo ${arrtest[13]}
The above would make an array named "arrtest" (the delcare -a line). It would then be told to contain all the words of the two lines in the file "test" as individual elements (the arrtest= line). The last line of the script would print the 13th element which would be the word "time" from the second line of the file. (The element numbers start at 1 and count from there. The [13] is called a subscript of the array arrtest.
 
Old 11-30-2005, 01:34 PM   #3
nkoplm
Member
 
Registered: May 2005
Distribution: Fedora
Posts: 92

Original Poster
Rep: Reputation: 15
thanks for the help, but i think i need to clarify

i did not want a 2d array, just simply an array like this for "test":

MYARRAY[0] = "the quick brown fox jumped over the lazy dog"
MYARRAY[1] = "now is the time for all good men to come to the aid of their country"

so on and so forth..
 
Old 12-01-2005, 07:51 AM   #4
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
What you want to do is go in a loop, assign each line to one element of array & keep incrementing the subscripts. The way to assign some value to to the nth subscript of array arr is:

Code:
arr[n-1]=value
HTH
 
Old 12-01-2005, 08:47 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
this does it in perl.

Code:
#!/usr/bin/perl -w

@words = <>;
$total = scalar @words;
print $words[ int rand($total) ];
Code:
billym.>./random_word.pl /usr/dict/words
Dixieland
 
Old 12-01-2005, 08:49 AM   #6
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Rep: Reputation: 15
Hai buddy-- how about this
Code:
#!/bin/bash
declare -a arrtest
count=`wc -l trial3 | cut -f 1 -d " "`
j=0;
i=`expr $count`
for((; i>=0 ; i--))
do
   arrtest[$j]=`tail -$i trial3 | head -1`
    j=`expr $j + 1`
done
rand1=$RANDOM
rand2=`expr $rand1 % $count`
echo ${arrtest[$rand2]}
Cheers
 
Old 12-01-2005, 08:57 AM   #7
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Billy, You can't use "the 800 pound gorilla"....
 
Old 12-01-2005, 09:03 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
aw why not?
look at that! 3 lines!
lovely.
 
Old 12-01-2005, 09:30 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
Ok then, how about Korn?
only 4096 in an array though.

Code:
#!/bin/ksh

x=$(cat $1)
set -A list $x
count=$(echo $x | wc -w )
echo ${list[$(( RANDOM % count ))]}
Code:
billym.primadtpdev>ls | ./random_word.ksh 
kbhit
billym.primadtpdev>ls | ./random_word.ksh
write.c
billym.primadtpdev>ls | ./random_word.ksh
eggs
 
Old 12-01-2005, 09:36 AM   #10
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
The problem is that the poster wants random "line" instead of words. The code may not remain that small if you want to dispaly randomly selected full line using arrays.

Regards.
 
Old 12-01-2005, 09:43 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
I then refer you to the perl solution.

 
Old 12-02-2005, 07:48 AM   #12
vivekr
Member
 
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68

Rep: Reputation: 15
Big.. and dustu mine is a code that would solve the problem I think. Why dont u try that
 
Old 12-02-2005, 08:09 AM   #13
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
@vivekr: I'm sure your code does the job. I was just wishfully wondering aloud about doing it using array in a "clever & small" shell script. Billy did it in "perl" anyway.

I think we have already answered the OPs query.

Regards.
 
Old 12-02-2005, 09:09 AM   #14
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,028
Blog Entries: 5

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
O.P.?

Ron Howard was asking the question?
 
Old 12-02-2005, 10:50 AM   #15
Brain Drop
Member
 
Registered: Feb 2003
Location: just outside reality
Distribution: balanced
Posts: 752

Rep: Reputation: 35
Poor neglected tcl

Code:
#!/usr/bin/tcl
set i 0

while { [ gets stdin line ] >= 0 } {
        set aline($i) $line
        incr i
        }
while { 1 } {
        set j [ expr int( [ expr rand() ] * 10 ) % $i ]
        puts $aline($j)
        after 500
        }
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 script to delete files c0d3 Programming 9 12-05-2004 11:45 PM
bash script on manipulating files fiomba Linux - Software 8 10-30-2004 09:31 PM
bash script - variables & arrays question rblampain Linux - Software 4 09-25-2004 10:57 AM
Problem in C: arrays, functions and strings OrganicX Programming 15 03-18-2003 10:31 AM
bash script to rm all files in a dir keirobyn Programming 8 07-19-2002 08:53 AM


All times are GMT -5. The time now is 02:04 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration