You're viewing a comment by Peter Krumins and its responses.

May 08, 2013, 17:39

Hi Stuart,

I've been busy so forgive me my late reply.

You can solve this problem with a Perl one-liner but it gets a bit complicated. It's best if you moved to writing Perl programs at this point rather than one-liners.

Does each email gets saved as a separate msg*.txt file? If so you can do the following:

perl -e "
  my @files = <msg*.txt>;
  open my $out, '>', 'spam-emails.txt' or die 'failed opening spam-emails.txt: ' . $!;

  for my $file (@files) {
    my $contents = do {
      open my $fh, '<', $file or die 'error opening ' . $filename . ': ' . $!;
      local $/; <$fh>
    };
    $_ = $contents;
    if (/spam_keyword_1/ && /spam_keyword_2/ && /spam_keyword_3/) {
      print $out qq/$file\n/;
    }
  }
"

What happens here is that we first setup @files array to contain the filenames of all emails in msg*.txt files.
Then we open the 'spam-emails.txt' file that will contain the list of filenames that are spam emails.
Next we loop over the filenames and read the contents of each email into $contents variable.
Then we check if the contents matches spam_keyword_1, spam_keyword_2, spam_keyword_3, and if so we print the filename of the spam email to spam-emails.txt.

You can create your own list of spam_keywords.

Reply To This Comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "security_303": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.