Retrieve email subject from file via bash -
i've shell script downloads files servers mail folder nas devices client has copies made locally backup.
the files saved 11469448248.h15587p19346.smtp.x14.eu_2,s files. i've changed extension standard .eml format email clients can read them disc.
for f in *.smtp.x14.eu_2,s; #sed "9q;d" $f #tail -n+9 $f | head -n1 mv -- "$f" "${f%.smtp.x14.eu_2,s}.eml"; done
as can see i've tried use sed , tail command 9th line file; problem subject isn't on 9th line , file names don't of content.
i'm trying files names in understandable format, figured subject helpful.
on nth line of email file line begins subject: pd: subject
im trying find line fet rid of subject: pd: , leave rest ne file name
the following wrong implements seem asking.
subj=$(sed -n '/^subject: pd *//!d;p;q;/^$/q' "$f")
the problem succeeds in trivial case, fails when have mime rfc2047-encoded subject:
header, , (more trivially) when subject:
header spans more single line.
i approach more modern programming language. it's not quite one-liner, it's easy enough python.
subj=$(./emailsubj.py "$f")
where emailsubj.py
contains more or less like
#!/usr/bin/env python email.parser import parser email.header import header, decode_header sys import argv filename in argv[1:]: open(filename, 'rb') handle: # handle file not found etc? message = parser().parse(handle) try: subj = ''.join([frag.decode(enc) if enc else frag frag, enc in decode_header(message['subject'])]) except headerparseerror, unicodedecodeerror: subj = message['subject'] # maybe warn error? print(subj)
(remember chmod +x emailsubj.py
, obviously.)
Comments
Post a Comment