-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsemail
More file actions
executable file
·32 lines (27 loc) · 1.02 KB
/
Copy pathparsemail
File metadata and controls
executable file
·32 lines (27 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/sh
# TODO:
# - multiple 'to' addresses: parse only until first comma (first address)
# - print usage
# Usage:
# `parsemail [PATH_TO_MAIL_FILE]
# Returns tab-separated result in following order:
# TO, FROM, SUBJECT, PRIORITY
# awk commands:
# line 1: replace first colon by tab to split into 2 columns by first colon.
# line 6: return tab-separated results
DECODE_HEADER_PYTHON='import email.header; import sys; bytes, enc = email.header.decode_header(sys.stdin.read())[0]; print(bytes.decode(enc) if enc else bytes.replace("\n", ""))'
cat "${1--}" | sed 's/\t/ /g' | # replace tabs with whitespaces first
awk -F '\t' '
{ sub(/: /, "\t") }
$1 == "To" { to = $2 }
$1 == "From" { from = $2 }
$1 == "Subject" { subject = $2 }
$1 == "X-Priority" { priority = $2 }
END { print to "\t" from "\t" subject "\t" priority }
' | while IFS="$(printf '\t')" read -r TO FROM SUBJECT PRIORITY; do
printf "%s\t%s\t%s\t%s\n" \
"$TO" \
"$FROM" \
"$(printf "%s" "$SUBJECT" | python -c "$DECODE_HEADER_PYTHON")" \
"$PRIORITY"
done