Skip to content

Instantly share code, notes, and snippets.

@Ephigenia
Created October 18, 2011 11:33
Show Gist options
  • Select an option

  • Save Ephigenia/1295219 to your computer and use it in GitHub Desktop.

Select an option

Save Ephigenia/1295219 to your computer and use it in GitHub Desktop.
random-email-from-file
grep -Z -P '^From:s(.+)' mail_export.txt | uniq
#!/bin/bash
##########################################################
# Extract n radom emails from a file
#
# Usage:
# winner.sh [filename] [count]
#
# Author: Marcel Eichner // Ephigenia <love@ephigenia.de>
# Date: 2009-03-30
##########################################################
TMPFILE=`mktemp -t emails` || exit 1
grep -Z -P '^From:s(.+)' "$1" | uniq > $TMPFILE
# select random line from file
LCOUNT=`wc -l "${TMPFILE}" | awk ' { print $1; }'`
echo "Choosing ${2:-1} winners from ${LCOUNT} emails"
for (( i = 0 ; i < ${2:-1}; i++ ))
do
LINENUMBER=$((($RANDOM % $LCOUNT) + 1))
sed -n ${LINENUMBER}p ${TMPFILE}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment