LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 10-06-2004, 05:55 AM   #1
ericcarlson
Member
 
Registered: Jan 2002
Posts: 161

Rep: Reputation: 30
Bash - choose a random line from a file


You guys are *ace* at these kind of problems! Suppose I have a text file like this:

Apples
Pears
Oranges
Banannas

And I want to set a variable in a bash script with a random word from this file. Possible? Easy? Who knows ;-) !!!
 
Old 10-06-2004, 06:29 AM   #2
320mb
Senior Member
 
Registered: Nov 2002
Location: pikes peak
Distribution: Slackware, LFS
Posts: 2,577

Rep: Reputation: 47
a bash script using awk/gawk can choose a random "file" from a directory.........

I'm sure it can be used to select a word at random from a file........
 
Old 10-06-2004, 06:39 AM   #3
ericcarlson
Member
 
Registered: Jan 2002
Posts: 161

Original Poster
Rep: Reputation: 30
The filename is known

And its a line I'm after, even though in my example I have 1 word/line.
Thanks for your interest.
 
Old 10-06-2004, 09:15 AM   #4
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE 13.1 / 12.3_64-KDE, Ubuntu 14.04, Fedora 20, Mint 17, Chakra
Posts: 3,689

Rep: Reputation: Disabled
cat filename | grep īpatternī?

<edit> ...solution for a question he didn't ask in the first place ... </edit>

Last edited by JZL240I-U; 11-10-2010 at 07:34 AM.
 
Old 10-06-2004, 09:41 AM   #5
sudipta_cht
LQ Newbie
 
Registered: Oct 2003
Location: Austin, Texas
Distribution: Ubuntu
Posts: 6

Rep: Reputation: 0
Not exactly a bash or a shell script, but if you can use a C file with it.. that just return a random number from 1 to a specific number you supply, and then you can get that line from the file.
 
Old 10-06-2004, 10:34 AM   #6
jmings
Member
 
Registered: Sep 2004
Location: Hemet,California, USA
Distribution: SimplyMEPIS
Posts: 31

Rep: Reputation: 16
Cool randline.sh

Here is randline.sh, a script to return a random line from a file.

wizard@3[~]$
wizard@3[~]$ cat test.dat
Apples
Pears
Oranges
Banannas
wizard@3[~]$ cat randline.sh
#!/bin/bash
if [ $# -ne 1 ]
then
echo "Syntax: $0 FILE"
echo $0 - display a random line from FILE.
exit 1
fi
RAND=`cat /proc/sys/kernel/random/uuid | cut -c1-4 | od -d | head -1 | cut -d' ' -f2`
LINES=`cat "$1" | wc -l`
LINE=`expr $RAND % $LINES + 1`
head -$LINE $1 | tail -1
wizard@3[~]$ ./randline.sh
Syntax: ./randline.sh FILE
./randline.sh - display a random line from FILE.
wizard@3[~]$ ./randline.sh test.dat
Pears
wizard@3[~]$ ./randline.sh test.dat
Apples
wizard@3[~]$ ./randline.sh test.dat
Oranges
wizard@3[~]$ ./randline.sh test.dat
Banannas
wizard@3[~]$ ./randline.sh test.dat
Banannas
wizard@3[~]$ ./randline.sh test.dat
Pears
wizard@3[~]$ ./randline.sh test.dat
Pears
wizard@3[~]$ ./randline.sh test.dat
Banannas
wizard@3[~]$ ./randline.sh test.dat
Apples
wizard@3[~]$
 
1 members found this post helpful.
Old 10-06-2004, 10:42 AM   #7
ericcarlson
Member
 
Registered: Jan 2002
Posts: 161

Original Poster
Rep: Reputation: 30
Wow! You win big style my man - thanks x 1000 :-)
 
Old 10-06-2004, 03:12 PM   #8
jmings
Member
 
Registered: Sep 2004
Location: Hemet,California, USA
Distribution: SimplyMEPIS
Posts: 31

Rep: Reputation: 16
You are welcome :-)
 
Old 03-23-2009, 01:59 PM   #9
gimly
LQ Newbie
 
Registered: Aug 2007
Posts: 2

Rep: Reputation: 0
Little addition to script

RAND can be more random and simple (no need cuts):
RAND=`od -d -N2 -An /dev/urandom`

Bash can simplify some equations (no need eval):
LINE=$(( RAND % LINES + 1 ))
 
Old 03-23-2009, 03:09 PM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
This thread is now 4.5 years old---I don't think anyone's listening anymore
 
Old 03-24-2009, 03:50 AM   #11
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE 13.1 / 12.3_64-KDE, Ubuntu 14.04, Fedora 20, Mint 17, Chakra
Posts: 3,689

Rep: Reputation: Disabled
Quote:
Originally Posted by pixellany View Post
...I don't think anyone's listening anymore
...except those who have a subscription .
 
Old 03-24-2009, 09:08 AM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
I won't even ask why you are still subscribed to this........
 
Old 03-24-2009, 10:46 AM   #13
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE 13.1 / 12.3_64-KDE, Ubuntu 14.04, Fedora 20, Mint 17, Chakra
Posts: 3,689

Rep: Reputation: Disabled
You honestly think I should unsubscribe all finished threads I once participated in? Do you do that with yours? Then you have my deepest admiration and respect .
 
Old 04-07-2010, 10:56 AM   #14
mroman
LQ Newbie
 
Registered: Mar 2010
Posts: 1

Rep: Reputation: 0
Well . . .

Quote:
Originally Posted by pixellany View Post
This thread is now 4.5 years old---I don't think anyone's listening anymore
Well, I don't mean to barge in to this exchange fellows however, I just found this thread through Google, and the script by jmings works perfect, his post illustrated what he did (I can't stand it when people just slap paste some code, I mean, I am not asking for a 100 page manual for a one liner, however, it truly does not hurt to illustrate things a bit) and I have benefited from this thread, and no, this is not some joke at the expense of pixellany, this is an authentic noob working on an evil little project of his

Cheers to all, and thank you jmings!

M.ROMAN
 
Old 04-08-2010, 02:55 AM   #15
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE 13.1 / 12.3_64-KDE, Ubuntu 14.04, Fedora 20, Mint 17, Chakra
Posts: 3,689

Rep: Reputation: Disabled
That's even more than I bargained for. Good luck to you mroman .
 
  


Reply

Tags
old, post, still


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 shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 09:16 PM
bash: read a line from file zokik Linux - General 6 12-10-2008 10:24 AM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 08:10 AM
BASH: How to change a line in file? gmitra Programming 4 01-28-2005 08:26 AM
PHP - read a random line from a file? Erik Thorsson Programming 3 12-10-2004 11:31 AM


All times are GMT -5. The time now is 02:37 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