从电子邮件中提取文本然后发送文本

时间:2014-06-24 23:14:37

标签: email awk sed grep sms

每当发生交易时,我都会在银行设置警报。我一直试图仅提取日期和金额,并将其作为短信转发给自己。

以下是警报电子邮件的内容:

FIRSTNAME LAST NAME

A transaction has been posted to your BANKNAME ACCOUNTNAME, and is within the parameters you set for triggering this alert.  

The transaction was on 06/20/2014 in the amount of ($40.00).  For recent account history, including transaction descriptions and running balances, sign on to BANKNAME Account Manager (online banking) and click on the account name.


BANKNAME Disclaimer: This transmittal is intended only for the use of the individual or entity to which it is addressed and may contain information that is privileged, confidential and exempt from disclosure under applicable law. If the reader of this transmittal is not the intended recipient, or the employee or agent responsible for delivering the transmittal to the intended recipient, you are notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender by e-mail and delete this message from your computer.

我能够使用grep,awk和sed,但只能显示整行。

:~# nawk '/The transaction was on/,/For recent account history/' alert.txt
The transaction was on 06/20/2014 in the amount of ($40.00).  For recent account history, including transaction descriptions and running balances, sign on to BANKNAME Account Manager (online banking) and click on the account name.

如何更改命令以仅提取日期和金额,以便结果看起来像这样:

06/20/2014 $40.00

计划是将该输出作为短信发送给我自己。

2 个答案:

答案 0 :(得分:1)

尝试

awk -vRS=\  '/[0-9]+\/[0-9]+\/[0-9]+/ {d=$0} /\$[0-9]+\.[0-9]+/ {print d, substr($0, 2, length - 3); exit}'

说明:

/[0-9]+\/[0-9]+\/[0-9]+/

Matches 1 or more digits, a slash, 1 or more digits, a slash, and 1 or more digits.

[0-9] matches a single digit character in 0, 1, 2, ..., 9
+     causes the previous entity to be matched 1 or more times
\/    is a literal slash (the backslash "escapes" it so it doesn't terminate
      the pattern)

/\$[0-9]+\.[0-9]+/

Matches a dollar sign, 1 or more digits, a period, and 1 or more digits.

\$ matches a literal dollar sign (a dollar sign is otherwise an anchor matching
   the end of the string)
\. matches a literal period (a period otherwise matches any character)

答案 1 :(得分:1)

您可以尝试使用以下grep命令获取日期和金额,

$ grep -oP '\d{2}\/\d{2}\/\d{4}|\$[^\)]*' file | paste -d' ' - -
06/20/2014 $40.00

您也可以在GNU sed

中执行此操作
$ sed -nr 's~^.*([0-9]{2}\/[0-9]{2}\/[0-9]{4}).*\((\$[^)]*)\).*$~\1 \2~p' file
06/20/2014 $40.00