Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
    0: #!/usr/bin/perl
    1: 
    2: # Simple program to remove duplicate email messages
    3: # from an mbox file. This program only looks at the content
    4: # of the message for uniqueness, not entire message with the headers.
    5: # There is no file locking, use this program on a backup 
    6: # of your mbox file.
    7: # Enjoy.
    8: 
    9: use strict;
    10: use warnings;
    11: use Digest::MD5 qw(md5_hex);
    12: 
    13: #grab file names from the program parameters.
    14: #and do some error checking.
    15: my ($from, $to) = @_;
    16: die "usage: $0 from to" unless (defined $from && defined $to);
    17: my (%uniq, $msg);
    18: my ($head, $body);
    19: my $i = 0;
    20: 
    21: $|++;
    22: 
    23: open (my $fh, "<$from") || die "cannot open $from: $!";
    24: while(<$fh>) {
    25: 	#emails in mbox files always begin with ^From 
    26: 	#when /^From / is matched, process the previous message
    27: 	#then start on this message
    28: 	if(m/^From /) {
    29: 		next if ($msg eq "");
    30: 		#increment the counter for a status report
    31: 		$i++;
    32: 		#print a status report if necessary.
    33: 		#I like to do it this way
    34: 		print '.' if(($i % 50) == 0);
    35: 		print " $i\n" if(($i % 1000) == 0);
    36: 		#since evolution can give different headers on the same message,
    37: 		#only hash the body of the message, and use that to compare to other
    38: 		#emails. The entire message will be stored in the hash though.
    39: 		($head, $body) = split /\n\n/, $msg;
    40: 		#standard perl technique for removing duplicates, using hashes and 
    41: 		#md5 files.
    42: 		$uniq{md5_hex($body)} = $msg;
    43: 		
    44: 		#done processing the previous message, start the next message
    45: 		$msg = $_;
    46: 	} else {
    47: 		#current line didn't match /^From / so this line is part of the
    48: 		#middle of the current message. Just tack it on to the end.
    49: 		$msg .= $_;
    50: 	}
    51: }
    52: 
    53: #print the results to a file.
    54: open (my $th, ">$to") || die "cannot open $to: $!";
    55: while(my ($k, $v) = each %uniq) {
    56: 	print $th $v;
    57: }

In reply to Remove Duplicates from a mbox file by coolmichael

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (7)
    As of 2015-07-08 00:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (93 votes), past polls