【问题标题】:How to parse email text (bash)如何解析电子邮件文本(bash)
【发布时间】:2015-05-08 23:57:29
【问题描述】:

我有以下文字(通过电子邮件收到):

----boundary_3_f515675d-c033-4705-a01e-244d1d6c8368
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

=0D=0ANew Lead from X Akows kl iut Sop=0D=0A=0D=0AName:=0D=0A Mic=
hael Knight=0D=0A =0D=0AEmail Address:=0D=0A <a href=3D"mailto:mi=
ck@emailaddress.co.uk">mick@emailaddress.co.uk</a>=0D=0A =0D=0ATelephon=
e:=0D=0A  00447783112974=0D=0A =0D=0AComments:=0D=0A Please send =
over more details =0D=0A=0D=0BBIOTS Reference:=0D=0A CV1614218=0D=0A=
=0D=0AYour Ref:=0D=0A 12194-109543=0D=0A=0D=0AView Property:=0D=0A=
 http://abropetisd.placudmnsdwlmn.com/CV1614218 =0D=0A=0D=0A =0D=0A=
 ----------------------------------------------------------------=
---------------=0D=0A=0D=0APlease note: You may not pass these de=
tails on to any 3rd parties.=0D=0AThis enquiry was sent to you by=
 X Akows kl iut Sop, txd UK?s #1 klsue fus kwhesena luhdsnry.  Vi=
sit www.placudmnsdwlmn.com for more information.=0D=0AQuestions? =
Email agents@placudmnsdwlmn.com=0D=0A
----boundary_3_f515675d-c033-4705-a01e-244d1d6c8368

我想解析它以获得某些信息。

我需要:

Name:
Email Address:
Telephone:
Comments:
Reference:
Your Ref:
View Property:

如何使用“bash”提取这些信息?

【问题讨论】:

  • 你试过什么?我不会使用bash。我会使用脚本语言,例如 perlawk...
  • 我已经尝试过 awk 但我无法让它工作!
  • 也许您可以展示您的尝试并获得帮助。
  • “您不得将这些细节传递给任何第三方。”在 Stackoverflow 上看到是一件好事。 :P
  • 我改了文字!详情不在上面公布的文字中!

标签: bash email mime quoted-printable


【解决方案1】:

好吧,我会咬人的。数据是可引用打印的,我们想要纯文本版本。所以让我们使用已经有代码的 Perl。

#!/usr/bin/perl

use strict;
use PerlIO::via::QuotedPrint;

# Open input file through quoted-printable filter    
$ARGV[0] ne "" or die "No file specified";
open(IN, '<:via(QuotedPrint)', $ARGV[0]) or die "Could not open file";

# needles to search in the haystack.
my @needles = ( 'Name',
                'Email Address',
                'Telephone',
                'Comments',
                'Reference',
                'Your Ref',
                'View Property' );

my $line;
my $key = "";

# handle the file linewise.
foreach $line (<IN>) {

    # The data we want is always one line after the
    # key line, so:

    # If we remember a key
    if($key ne "") {
        # print key and line, reset key variable.
        print "$key =$line";
        $key = "";
    } else {
        # otherwise, see if we find a key in the current line.
        # If so, remember it so that the data in the next line
        # will be printed.
        my $n;
        foreach $n (@needles) {
            if(index($line, $n) != -1) {
                $key = $n;
                last;
            }
        }
    }
}

将其放入文件中,例如 extract.plchmod +x 它,然后运行 ​​./extract.pl yourfile

【讨论】:

  • 做得很好。看起来每行最多需要 1 个密钥,因此可能在 $key = $p 之后放置一个 last; 以在找到密钥后终止循环。最后,一个小问题(实际上就是这样):“模式”暗示了一个正则表达式(或通配模式),但您正在搜索字符串 literals (您已将其命名为“key”找到匹配项)。
  • last; 是个好点;我不知道为什么我把它放在首位。至于模式,让我们用针(就像大海捞针一样)。
  • 需要说(如果你愿意的话),感谢您的更新。
  • @NeilReardon:为了回答者和未来读者的利益:如果答案解决了您的问题,请点击大检查接受在它旁边做标记;如果您觉得它至少有帮助,请点击向上箭头图标投票
【解决方案2】:

首先,感谢大家的帮助。

我找到了另一种方法,我想在这里发布。

sed -e 's/=C2=A0/ /g' abc.txt | perl -pe 'use MIME::QuotedPrint; $_=MIME::QuotedPrint::decode($_);' | grep "^Interested in:" | cut  -d' ' -f3-

sed -e 's/=C2=A0/ /g' abc.txt | perl -pe 'use MIME::QuotedPrint; $_=MIME::QuotedPrint::decode($_);' | grep "^Name:" | cut  -d' ' -f2-

我不知道为什么,但原始文本包含“=C2=A0”,这似乎与“ ”相同。所以我只是用“sed”把它们去掉。

最好的问候,

尼尔。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-01
    • 2012-04-06
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多