Skip to content

Instantly share code, notes, and snippets.

@briansanchez
Last active September 26, 2020 20:19
Show Gist options
  • Save briansanchez/c073bca39cd51e09b8d239987a971edc to your computer and use it in GitHub Desktop.
Save briansanchez/c073bca39cd51e09b8d239987a971edc to your computer and use it in GitHub Desktop.
bash script to extract emails from txt files
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
TXT_FILES_DIR=$DIR/txt
#check if directory is not empty
if [ "$(ls -A $TXT_FILES_DIR)" ]; then
#concatenate all text files in 1 log txt file
cat $DIR/txt/*.txt > $DIR/txt_done/log.txt_"$(date +%Y%m%d-%H%M%S)".txt
#extract emails from log.emails txt file
egrep -o "\b[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z0-9.-]+\b" $DIR/txt_done/log.txt_"$(date +%Y%m%d-%H%M%S)".txt > $DIR/txt_done/log.emails.txt_"$(date +%Y%m%d-%H%M%S)".txt;
#delete all txt files in directory
rm -Rf $DIR/txt/*.txt
#delete log file generated to find emails
rm $DIR/txt_done/log.txt_"$(date +%Y%m%d-%H%M%S)".txt
else
echo "empty directory"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment