Last active
December 9, 2016 13:32
-
-
Save caiquecastro/fa8457d551f04c204dcac78f6bbd783c to your computer and use it in GitHub Desktop.
Sort on shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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