On Sun, Aug 22, 2004 at 02:35:14PM -0500, Lance Hoffmeyer wrote:
> Hello all,
> 
> I would like to write a script that will select N number of
> random lines in a file.  Any suggestions on how to do this?
> 
> Lance

One way:

- Count lines in file
- Pick N line numbers
- Show file, filtering out unpicked lines

# implementation

rnd () {
   perl -e 'print int(rand()*'$1'), "\n"'
}

LCT=`wc -l < "$2"`

PAT=tommy

for x in `seq 1 "$1"`; do
   PAT="$PAT\\|$(rnd $LCT)"
done

sed = < "$2" | sed -n -e 'N; s/\
/ /; /^\('$PAT'\) /p' | sed 's/[^ ]* //'

Another way:

#! /usr/bin/perl

@lines = <STDIN>;

for ($i = 0; $i < $ARGV[0]; $i++) {
    print $lines[ rand @lines ];
}

Endless variations...


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to