Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Not Quite perl...

by Anonymous Monk
on May 09, 2003 at 13:11 UTC ( [id://256849]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

OK, Spank me. This question is not quite related to perl, but i know how inteligent you guys all are, and i would realy appreciate your input. I have my main mailbox file at /var/mail/james which when i open it, is simply a plain text file containing a few e-mails, and i have a backup of my file before my machine went down, which i have put at /root/james. I want to merge these two files together i.e put the contents of /root/james into the file /var/mail/james, is there a way to merge these two?

Replies are listed 'Best First'.
Re: Not Quite perl...
by Limbic~Region (Chancellor) on May 09, 2003 at 13:23 UTC
    Anonymous Monk,
    While smitz has already pointed out you can do:

    cat /root/james >> /var/mail/james

    You can also do this with Perl.

    #!/usr/bin/perl -w use strict; open (MBOX1,"/root/james") or die "unable to open mailbox : $!"; open (MBOX2,">>/var/mail/james") or die "unable to append to mailbox : + $!"; select MBOX2; while (<MBOX1>) { print; }
    Of course - don't do this - you would be in a race condition. If any other process were writing to the second mailbox, information would become inter-leaved. Yes, there is locking, but I do not believe Sendmail respects locks on mailboxes.

    You could always check CPAN - Mail::Box::Mbox and Mail::Box were both updated yesterday 8 May 03.

    Cheers - L~R

Re: Not Quite perl...
by smitz (Chaplain) on May 09, 2003 at 13:21 UTC
    If /var/mail/james is empty or doesnt exist
    • cat /root/james > /var/mail/james
    If not
    • cat /root/james >> /var/mail/james

    This code is untested, and I'm a windows kind of a guy, so test this on some expendable text files first :-)

Re: Not Quite perl...
by crouchingpenguin (Priest) on May 09, 2003 at 16:56 UTC

    As Limbic~Region pointed out, Mail::Box is perfect for this sort of thing. Here is an example (assuming mbox format):

    #!/usr/bin/perl use strict; use warnings; use diagnostics; use Mail::Box::Manager; my ($folder1,$folder2,$folder3) = @ARGV; my $mgr = Mail::Box::Manager->new( folderdir => [ '.' ] ); my $f1 = $mgr->open(folder => $folder1, ) or die $!; my $f2 = $mgr->open(folder => $folder2, ) or die $1; my $f3 = $mgr->open(folder => $folder3, create => 1, access => 'rw', ) + or die $!; foreach my $msg ( $f1->messages(),$f2->messages() ){ $msg->copyTo($f3) if $msg; } # or $mgr->copyMessage($f3, ($f1->messages(),$f2->messages()) ); $mgr->closeAllFolders(); 1;

    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
Re: Not Quite perl...
by teabag (Pilgrim) on May 09, 2003 at 13:29 UTC
    Whack! (sound of a profound spanking)
    Hmm, Im not sure about the date sorting so I would do it like this to be sure:
    cat /var/mail/james/mailfile >>/root/james/oldmailfile

    then copy it to your /var/mail/james/mailfile
    Archive the old /var/mail/james/mailfile just be be sure before you copy it there ;)

    Sure there's more than one way, but one just needs one anyway - Teabag

Re: Not Quite perl...
by smitz (Chaplain) on May 09, 2003 at 13:12 UTC
    man cat
      huh? I think it was a cat.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://256849]
Approved by benn
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-07-18 02:24 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.