Skip to content

Instantly share code, notes, and snippets.

@caiquecastro
Last active December 9, 2016 13:32
Show Gist options
  • Save caiquecastro/fa8457d551f04c204dcac78f6bbd783c to your computer and use it in GitHub Desktop.
Save caiquecastro/fa8457d551f04c204dcac78f6bbd783c to your computer and use it in GitHub Desktop.
Sort on shell
#!/usr/bin/env sh
# Prints a random line from a file
# Script by @augustohp
lines_on() {
file=$1
wc -l $file | awk '{print $1}'
}
random_until() {
max=$1
min=1
echo $(( ($RANDOM % $max) + $min ))
}
print_line_in_file() {
file=$1
line=$2
awk "NR==${line}" $file
}
if [ -z "$1" ]
then
echo "Usage: $0 <file>"
exit 2
fi
# Here be dragons
print_line_in_file $1 $(random_until $(lines_on $1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment