#!/usr/local/bin/perl
#
# randomline [-n] file... > random_file
#
# Simple perl program to strip all comments from a given file
# and then pick n lines at random. (n = 1 by default)
#
# Comments are either
#    1/  A line starting with `!' which doesn't interfer with cpp
#    2/  or from the character # to the end of line
#
srand(time);

$number = 1;  # default number of lines
if ( $ARGV[0] =~ /-\d+/ ) {
  $number = - shift;
}

while(<>) {
  next  if  /^!/;   # skip the ! comments (for cpp)
  s/#.*$//;         # remove any comment ( # to end of line )
  next unless /\S/; # try again if result is a blank line
  push(@lines, $_);
}

while( $#lines >= 0 && $number-- > 0 ) {
  print splice(@lines, rand(@lines)+$[, 1);
}

