LinuxQuestions.org
Review your favorite Linux distribution.
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 11-10-2010, 07:23 AM   #16
thomasvs
LQ Newbie
 
Registered: Dec 2006
Posts: 2

Rep: Reputation: 0
one-liner


cat lines | sort --random-sort | head -n 1

Gives you one random line for the input file lines.
 
Old 11-10-2010, 07:28 AM   #17
++nick++
Member
 
Registered: Dec 2008
Location: Bellevue,WA
Distribution: RHEL 5 , Fedora ,Sabayon,Solaris,Vmware,AWS
Posts: 107

Rep: Reputation: 18
whatever you do head -n 1 will output the first line??
 
Old 11-10-2010, 08:49 AM   #18
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Well, now we've raised Lazarus, an alternative solution
Code:
#!/bin/bash

file=LQ-239334.txt
line_count=$( wc -l < "$file" )
line_index=$(( RANDOM % line_count ))
IFS=$'\n'
lines=( $( cat "$file" ) )
unset IFS
echo "${lines[ line_index ]}"
EDIT: but thomasvs' solution is sweeter

EDIT 2:

And here's an improved version of the script which might run faster than thomasvs' solution, at least for small input files, say less than 4 kB.
Code:
#!/bin/bash

file=LQ-239334.txt
line_count=$( wc -l < "$file" )
line_index=$(( RANDOM % line_count ))
IFS=$'\n'
lines=( $( < "$file" ) )
unset IFS
echo "${lines[ line_index ]}"

Last edited by catkin; 11-10-2010 at 08:54 AM.
 
Old 12-10-2010, 01:53 AM   #19
brixtoncalling
Member
 
Registered: Jul 2008
Location: British Columbia
Distribution: Slackware current
Posts: 403

Rep: Reputation: 67
Cheers catkin for the latest solution.

See you back in slackware-14.
 
Old 05-14-2011, 03:48 PM   #20
mikeb1986
LQ Newbie
 
Registered: Mar 2011
Posts: 9

Rep: Reputation: 0
Sorry to drag up such an old thread, this is just what I wanted

That first script from jmings is great, thanks.

I wonder if someone would expand on one of the scripts, so it displays the word, then when I press return again, it displays the same line but from another text file.
EG, I run it, it displays line 28 from my Japanese vocabulary text file, I press return again and it displays line 28 from the translations text file

Thanks
 
Old 05-14-2011, 04:00 PM   #21
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Even simpler: shuf -n 1 <file_name> does it in one command. Try a man -k random | grep '(1' for a list of all stand-alone commands related to random things. (Provided you've installed the man system, which not all distribution default to doing.)
 
1 members found this post helpful.
Old 09-03-2012, 05:05 AM   #22
MoonCactus
LQ Newbie
 
Registered: Sep 2012
Posts: 1

Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by PTrenholme View Post
Even simpler: shuf -n 1 <file_name> does it in one command. Try a man -k random | grep '(1' for a list of all stand-alone commands related to random things. (Provided you've installed the man system, which not all distribution default to doing.)
Oh, great! As already said, unix is trivial once you know the 1000s basic commands

With sed and bash, I was previously using this one-liner:
Code:
sed -n $(( 1 + $RANDOM % $(wc -l file_name|cut -d' ' -f1) ))p .file_name
It may be useful when shuf is not available in your distribution.

Note that the awk syntax is shorter but slower for huge files (as checked with "/usr/bin/time -v <command>" on a huge input file)
Code:
awk NR==$(( 1 + $RANDOM % $(wc -l file_name|cut -d' ' -f1) )) .file_name

Last edited by MoonCactus; 09-03-2012 at 05:29 AM. Reason: typo
 
Old 09-04-2012, 01:49 AM   #23
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Hmm, I never noticed this old thread in the several times it's come up? How odd. I do remember posting in one very much like it though, perhaps 2-3 years ago.

Anyway, since then I've discovered this BashFAQ entry:

How can I randomize (shuffle) the order of lines in a file? (Or select a random line from a file, or select a random file from a directory.)
http://mywiki.wooledge.org/BashFAQ/026
 
Old 09-04-2012, 03:00 AM   #24
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by MoonCactus View Post
With sed and bash, I was previously using this one-liner:
Code:
sed -n $(( 1 + $RANDOM % $(wc -l file_name|cut -d' ' -f1) ))p .file_name
I was about to show that solution when I saw this thread. Anyway you don't need to use cut
Code:
FILE=filename.ext
sed -n "$(( RANDOM % $(exec wc -l < "$FILE") + 1 ))p" "$FILE"

Last edited by konsolebox; 09-04-2012 at 03:12 AM.
 
Old 11-11-2013, 05:16 PM   #25
islandBilly
LQ Newbie
 
Registered: Dec 2007
Location: Bahamas
Distribution: Ubuntu 11.04
Posts: 17

Rep: Reputation: 0
What about the script variable??

This is an old thread, but it seems to have some staying power. As far as I can see, nobody has answered a crucial part of the question, which is how do you get the output of these fancy commands into a freakin script variable?? That is what I need. I can do the random selection of a line in a file, but how to get the output of that or any shell command into a script variable??

Please?
 
Old 11-11-2013, 05:22 PM   #26
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
@islandBilly: Not really the place to ask this (re-opening an old thread). A new thread would have been better.

That said: To store the output of a command in a variable:
Code:
VARIABLE="$( your_command_goes_here )"

# this also works but the above is preferred:
VARIABLE="` your_command_goes_here `"
 
1 members found this post helpful.
Old 11-11-2013, 05:59 PM   #27
islandBilly
LQ Newbie
 
Registered: Dec 2007
Location: Bahamas
Distribution: Ubuntu 11.04
Posts: 17

Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
@islandBilly: Not really the place to ask this (re-opening an old thread). A new thread would have been better.

That said: To store the output of a command in a variable:
Code:
VARIABLE="$( your_command_goes_here )"

# this also works but the above is preferred:
VARIABLE="` your_command_goes_here `"
thank you very much - er, what time is it in Nederlands? You're up late I think!
 
Old 11-12-2013, 02:25 AM   #28
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
In case the question pops up again who is still listening to old threads... .
 
  


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 04:01 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