Skip to content

Instantly share code, notes, and snippets.

@ChristopherA
Last active March 8, 2017 16:41
Show Gist options
  • Save ChristopherA/38339c17981bdb664860f1c63cc6e52b to your computer and use it in GitHub Desktop.
Save ChristopherA/38339c17981bdb664860f1c63cc6e52b to your computer and use it in GitHub Desktop.
Extract Email Shell Script
#!/bin/bash
#Works as both "./extractemail.sh filename" and "cat filename | ./myscript.sh"
# Set variable input_file to either $1 or /dev/stdin, in case $1 is empty
# Note that this assumes that you are expecting the file name to operate on on $1
# # This version doesn't test if the input is a valid filename
# input_file="${1:-/dev/stdin}"
# If the script argument is provided and it is a file, then variable input_fille is
# assigned to the file name. If there is no valid argument then reads from stdin
[ $# -ge 1 -a -f "$1" ] && input_file="$1" || input_file="-" ]
# You can now use "$input_file" as your file to operate on
#grep from http://emailregex.com/
grep -E -o "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" $input_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment