#!/usr/bin/perl # source: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=961638 =head1 NAME stocat - stochastic cat, selecting lines with uniform probability =head1 SYNOPSIS =over =item B [B<-p>|B<--probability> PROBABILITY] [I|B<->]... =back =head1 DESCRIPTION Concatenate FILE(s) to standard output, but printing each input line to output only with a given probability, defaulting to 0.1 (i.e., 10%). With no FILE or when FILE is B<->, read standard input. =head1 OPTIONS =over 4 =item -p, --probability Output lines with the given probability, specified as a number between 0 (0% probability) and 1 (100% probability). Default: 0.1 (i.e., 10% probability). =back =head1 SEE ALSO L =head1 AUTHOR Copyright 2020 by Stefano Zacchiroli Licensed under the GNU GPL. =cut use Getopt::Long; sub die_usage() { die "Usage: $0 [--probability|-p PROBABILITY] [file|-]\n"; } my $probability = 0.1; if (! GetOptions("probability|p=f" => \$probability)) { die_usage(); } if ($probability < 0 || $probability > 1) { die_usage(); } while (<>) { print $_ if rand() <= $probability; }