LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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
 
LinkBack Search this Thread
Old 11-10-2010, 06: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, 06:28 AM   #17
++nick++
Member
 
Registered: Dec 2008
Location: Chennai
Distribution: RHEL 5 , Fedora ,Sabayon,Solaris,Vmware
Posts: 105

Rep: Reputation: 18
whatever you do head -n 1 will output the first line??
 
Old 11-10-2010, 07:49 AM   #18
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,987
Blog Entries: 25

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
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 07:54 AM.
 
Old 12-10-2010, 12:53 AM   #19
brixtoncalling
Member
 
Registered: Jul 2008
Location: British Columbia
Distribution: Slackware current
Posts: 403

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

See you back in slackware-14.
 
Old 05-14-2011, 02: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, 03:00 PM   #21
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 3,666

Rep: Reputation: 204Reputation: 204Reputation: 204
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.)
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 48 05-31-2012 11:13 PM
bash: read a line from file zokik Linux - General 6 12-10-2008 09:24 AM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 07:10 AM
BASH: How to change a line in file? gmitra Programming 4 01-28-2005 07:26 AM
PHP - read a random line from a file? Erik Thorsson Programming 3 12-10-2004 10:31 AM


All times are GMT -5. The time now is 12:39 AM.

Main Menu
 
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
Open Source Consulting | Domain Registration