Naive sampling of file for bash/zsh

1 min read Original article ↗
# Sample file
samplef() {
# set -x
if [ -z "${1}" ]; then
echo 'Error: Missing file path'
echo
echo 'Usage:'
echo 'samplef <FILEPATH> <SAMPLE_SIZE>'
echo
echo 'Optional paramters: <SAMPLE_SIZE>, default is 0.1'
echo 'ex; samplef ./myfile.txt 0.25'
return
fi
SAMPLE_RATIO=${2:-0.1}
cat $1 | perl -n -e "print if (rand() < $SAMPLE_RATIO)"
}