* Bret Hughes [Sun, Feb 18, 2001 at 09:01:39PM -0600]:
>  
> *************************** random_line.sh *******************
> #!/bin/bash
> 
> ## 
> ## Name:
> ##    random_line.sh
> ##
> ## Version:
> ##    $Revision: 1.2 $
> ##
> ## Purpose:
> ##    Print a random line from a specified file.
> ##
> ## Usage:
> ##    random_line.sh <filename>
> ##
> ## Author:
> ##    Todd A. Jacobs <[EMAIL PROTECTED]>
> ##
> 
> [ "$#" = "0" ] && {
>         echo "usage: random_line.sh <filename>"
>         exit -1
> }
> 
> [ ! -f "$1" ] && {
>         echo "$1: No such file."
>         exit -2
> }
> 
> 
> 
> # Get number of lines in file.
> _LINES=$(wc -l < $1)
>       
> # Pick a random line number.
> _RANDOM_LINE=$(( $RANDOM % $_LINES + 1 ))
> 
> # Deliver a single line of output.
> head -n $_RANDOM_LINE < $1 | tail -n 1
> 
> ******************************************************************
> 

All that can be done with a perl one-liner in a much more efficient
way:

  perl -e 'rand;rand($.)<1&&($l=$_)while<>;print $l;' < file

It works. Believe me.


-- 
Johannes Eriksson <[EMAIL PROTECTED]>
Computer Science, AA University



_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to