Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Implement shell script to extract date from file contents in Automator

I've hacked together a bash script that I'm testing on a folder of .eml files. It extracts the first instance of a date string such as "30/01/15" from within the email, reformats the string to "15-01-30" and adds it to the beginning of the filename separated by an underscore.


for f in *.eml; do

date=`grep -o -m 1 '..\/..\/..' "$f" | awk -F "/" '{print $3"-"$2"-"$1}'`

name="${f%.*}"

ext="${f##*.}"

mv "$f" "${date}_${name}.$ext"

done


It works for the most part. On my test folder I got a few files named with garbled date output like "dm-4X-UG" but I haven't looked into that yet.


Main question: how do I go about implementing this as an automator service? I'd like to be able select files (.eml and .pdf mostly) that I know have this date format in the finder and run the script as a service. Can't seem to get it right.


Also, any suggestions for improving this? Not sure if it's the most efficient / safest script. I'd eventually like to write a bunch of these for some common date formats that show up in my email or OCR pdf receipts.


Thanks!

MacBook Pro with Retina display, OS X Yosemite (10.10.3)

Posted on May 25, 2015 6:55 PM

Reply
Question marked as Best reply

Posted on May 25, 2015 9:39 PM

Ok, got this working in Automator.


Also figured out it was garbling dates from finding a pattern match in the source code of the email due to using "." and searching for any character. Changed to [0-9].


I pieced this together from bits I found elsewhere, mostly here:


http://www.noodlesoft.com/forums/viewtopic.php?f=4&t=1530&sid=d707af8a13bafc1129 63c809719f7a72




User uploaded file

1 reply
Question marked as Best reply

May 25, 2015 9:39 PM in response to m_k_e

Ok, got this working in Automator.


Also figured out it was garbling dates from finding a pattern match in the source code of the email due to using "." and searching for any character. Changed to [0-9].


I pieced this together from bits I found elsewhere, mostly here:


http://www.noodlesoft.com/forums/viewtopic.php?f=4&t=1530&sid=d707af8a13bafc1129 63c809719f7a72




User uploaded file

Implement shell script to extract date from file contents in Automator

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.