Skip to content

Instantly share code, notes, and snippets.

@gautiermichelin
Created May 11, 2015 13:00
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gautiermichelin/358a819557cb3b88dac6 to your computer and use it in GitHub Desktop.
Save gautiermichelin/358a819557cb3b88dac6 to your computer and use it in GitHub Desktop.
Eml2csv : shell script to extract To/From/Subject/Date from eml files inside a folder
#!/bin/bash
FILES=*.eml
TARGET=./emails.csv
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
echo "To\tFrom\tSubj\tDate" >> $TARGET
for f in $FILES
do
echo "Processing $f file..."
# take action on each file. $f store current file name
EMLTO=$(grep "^To" < $f | sed -e "s/To://g")
EMLFROM=$(grep "^From" < $f | sed -e "s/From://g")
EMLSUBJ=$(grep "^Subject" < $f | sed -e "s/Subject://g")
EMLDATE=$(grep "^Date" < $f | sed -e "s/Date://g")
echo -e "${EMLTO//[$'\t\r\n']}\t${EMLFROM//[$'\t\r\n']}\t${EMLSUBJ//[$'\t\r\n']}\t${EMLDATE//[$'\t\r\n']}" >> $TARGET
done
IFS=$SAVEIFS
@brendanmullan
Copy link

Thanks for posting this Gautier, was exactly what I was looking for! 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment