LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-19-2008, 04:13 PM   #1
pdklinux79
LQ Newbie
 
Registered: Jun 2008
Posts: 29

Rep: Reputation: 15
how to pick random file name from a list of filenames in a text file.


My problem is :

ls -l /data1/cur/*.cdt >>fi.txt

this file has list of column names

i want some way to pick a random name from this fi.txt.

how to do it in bash shell scripting?

thanks
 
Old 06-19-2008, 05:34 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Could try
Code:
a=($(< fi.txt)); rand () { i=`od -cN10 /dev/random | sha1sum`; i=${i// */}; i="${i//[^0-9]/}"; n=${#a[@]}; n=${#n}; i="${i:0:$n}"; [ $i -le ${#a[@]} ] && echo "${i}" || rand; }; for (( n = 0 ; n < 10; n++)); do echo ${a[${RANDOM:2:3}]}; done
. Looks a bit wack but that's because $RANDOM isn't as random as it should be. Hmm. Spose an easier method will come your way RSN ;-p
 
Old 06-19-2008, 05:35 PM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Code:
awk "NR==$(($RANDOM % $(wc -l fi.txt| awk '{print $1}')))" fi.txt
or to break it down into multiple stages:

Code:
nlines=$(wc -l fi.txt| awk '{print $1}')
rand=$((RANDOM % $nlines))
awk "NR==$rand" fi.txt

Last edited by Mr. C.; 06-19-2008 at 06:17 PM.
 
Old 06-19-2008, 05:38 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Yeah, *way* easier.
 
Old 06-19-2008, 06:15 PM   #5
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Quote:
Originally Posted by pdklinux79 View Post
My problem is :

ls -l /data1/cur/*.cdt >>fi.txt

this file has list of column names

i want some way to pick a random name from this fi.txt.

how to do it in bash shell scripting?

thanks
Just take the first line when sorted randomly:

Code:
sort -R fi.txt | head -n 1
You can pipe that through cut if you want just the filename.

Of course, this isn't truly random since it hashes the keys, so you will get the same chance of each file regardless of the number of times it occurs. Also, it's only pseudorandom, as are all answers not seeding from /dev/urandom.
 
Old 06-19-2008, 06:18 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Quote:
Originally Posted by rjlee View Post
Just take the first line when sorted randomly:

Code:
sort -R fi.txt | head -n 1

Ouch! Sorting is one of the most expensive operations possible.
 
Old 06-19-2008, 06:54 PM   #7
kenoshi
Member
 
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
Quote:
Originally Posted by Mr. C. View Post
Code:
awk "NR==$(($RANDOM % $(wc -l fi.txt| awk '{print $1}')))" fi.txt
or to break it down into multiple stages:

Code:
nlines=$(wc -l fi.txt| awk '{print $1}')
rand=$((RANDOM % $nlines))
awk "NR==$rand" fi.txt
This will work only if $rand doesn't return a 0, which it does eventually. Also, you don't need the awk for nlines.

E.g.
Code:
rand=$((RANDOM % $(wc -l < fi.txt)))
if [ "$rand" = "0" ]; then ((rand++)) ; fi
awk "NR==$rand" fi.txt
 
Old 06-19-2008, 07:13 PM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
The redirection is a clever way to avoid the filename as part of the output. Good call.

I ignored the 0 case, but shouldn't have. Instead, because the line numbers are 1 based vs. 0 based, we can bias by one:

rand=$((1 + (RANDOM % $nlines)))

With 10 lines, mod will give 0 to 9. Biasing by 1, yields 1 to 10, exactly what we want.
 
Old 06-20-2008, 11:38 AM   #9
pdklinux79
LQ Newbie
 
Registered: Jun 2008
Posts: 29

Original Poster
Rep: Reputation: 15
Thanks a ton guys... Its working and my friday at work is a cool breeze. Have agood weekend.
 
Old 06-20-2008, 02:46 PM   #10
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Quote:
Originally Posted by Mr. C. View Post
Ouch! Sorting is one of the most expensive operations possible.
Ah, but it's easier to read.

And depending on the size of the list of files, speed may not be an issue. Slow but readable and reliable is sometimes better than highly optimised, and if speed were essential then I would be using C or assembler…
 
  


Reply



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
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 01:42 PM
How-to list contents of text file in alphabetical order? sucram2g Linux - Software 1 02-19-2007 03:39 AM
Send emails to list in text file using RedHat9 MrEase Linux - Newbie 1 04-24-2004 07:35 PM
How to pick a string from a text file? sdandeker Linux - General 1 02-12-2004 04:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:19 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
Open Source Consulting | Domain Registration