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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-30-2005, 05:16 AM
|
#1
|
Member
Registered: May 2005
Distribution: Fedora
Posts: 92
Rep:
|
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?
|
|
|
11-30-2005, 09:05 AM
|
#2
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
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.
|
|
|
11-30-2005, 01:34 PM
|
#3
|
Member
Registered: May 2005
Distribution: Fedora
Posts: 92
Original Poster
Rep:
|
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..
|
|
|
12-01-2005, 07:51 AM
|
#4
|
Member
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153
Rep:
|
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:
HTH
|
|
|
12-01-2005, 08:47 AM
|
#5
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
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
|
|
|
12-01-2005, 08:49 AM
|
#6
|
Member
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68
Rep:
|
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
|
|
|
12-01-2005, 08:57 AM
|
#7
|
Member
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153
Rep:
|
Billy, You can't use "the 800 pound gorilla"....
|
|
|
12-01-2005, 09:03 AM
|
#8
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
aw why not?
look at that! 3 lines!
lovely.
|
|
|
12-01-2005, 09:30 AM
|
#9
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
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
|
|
|
12-01-2005, 09:36 AM
|
#10
|
Member
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153
Rep:
|
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.
|
|
|
12-01-2005, 09:43 AM
|
#11
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
I then refer you to the perl solution.
|
|
|
12-02-2005, 07:48 AM
|
#12
|
Member
Registered: Nov 2005
Location: Coimbatore,India
Distribution: Fedora Core4
Posts: 68
Rep:
|
Big.. and dustu mine is a code that would solve the problem I think. Why dont u try that
|
|
|
12-02-2005, 08:09 AM
|
#13
|
Member
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153
Rep:
|
@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.
|
|
|
12-02-2005, 09:09 AM
|
#14
|
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
|
O.P.?
Ron Howard was asking the question?
|
|
|
12-02-2005, 10:50 AM
|
#15
|
Member
Registered: Feb 2003
Location: just outside reality
Distribution: balanced
Posts: 752
Rep:
|
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
}
|
|
|
All times are GMT -5. The time now is 06:05 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|