1832338 Members
2281 Online
110041 Solutions
New Discussion

tool to edit mailfile

 
SOLVED
Go to solution
Raymond Ford
Advisor

tool to edit mailfile

We need to develop a tool to automate deletion of mail that accumulates in root's mailfile based upon the Subject line. There are about 6 different subject lines to be deleted each time they occur and the mail arrives from 15 servers. We have tried the ex and ed editors but haven't succeeded. A concern is that should any script we come up with using the editors run inefficiently we could lose incoming emails while it runs. There are maybe 800 emails to check each day, and losing another SysAdmin makes our task more urgent. Thank-you to all in advance who have time to respond.
I'm an apprentice among journeymen who spend a lot of time on the virtual road.
9 REPLIES 9
Shahul
Esteemed Contributor

Re: tool to edit mailfile

Hi

I hope sed command will help U for this
#sed /^/,/^/d mail file


Here Mail header and footer U will have to give.

U can even specify from mail header to next blank line, or till 50 lines..etc. As per ur requirement U can customize..

I hope this will do for U.

Best of luck
Shahul
S.K. Chan
Honored Contributor

Re: tool to edit mailfile

Sound like you're going to do some scripting/programming work, etc,etc anyway so instead of automating deletion of email why not just do mail filtering instead. For instance you can filter by "subject" and when the email with that particular subject arrives it gets archive in a separate directory instead of appending to the mailbox. Here we use a procmail (www.procmail.org) and for example a filter template can be like so ..

http://www.uwasa.fi/~ts/info/proctips.html#start

I'm quoting example just to show you, it's not what we got setup here.

just a thought ..
Jordan Bean
Honored Contributor

Re: tool to edit mailfile

Go with S K Chan's recommendation to use procmail to filter during delivery. However, if you need to filter the messages already delivered, the attached perl script may help.

Caution: Study and test it first for I have not had opportunity to do so myself.

Frank Slootweg
Honored Contributor

Re: tool to edit mailfile

As others have mentioned, procmail is probably the best solution.

If you can not use procmail, you could use mailx(1) in a script, i.e.
for example

echo 'q' | mailx

will print a list of messages (which can then be grep(1)-ed for
Subjects: and for example

echo 'd 9\nq' | mailx

will delete message 9.

During development of the script, you can copy /var/mail/user to a file
and use "mailx -f file" so that you do not lose anything if your script
does not work right yet.
Raymond Ford
Advisor

Re: tool to edit mailfile

You've all been great to provide us with important components for a tool that "may" be working within a couple of days. I appreciate your time and thought on a task that turned out to be tougher than I anticipated. Any further comments will of course be appreciated.
I'm an apprentice among journeymen who spend a lot of time on the virtual road.
H.Merijn Brand (procura
Honored Contributor

Re: tool to edit mailfile

OK, if you insist :)

since your problem is more complex than your original question made me think it was, I'll direct you to use perl and choose any of the available Mail modules from CPAN.

For mail modules you could start here:
ftp://ftp.funet.fi/pub/languages/perl/CPAN/modules/00modlist.long.html#ID19_MailandUse and pick the module that fits best to your needs
Enjoy, Have FUN! H.Merijn
Raymond Ford
Advisor

Re: tool to edit mailfile

Thanks to everyone's help we came up with the attached script. Our final need is to have mail deleted that contains no subject line. In the "for SUB in" line can anyone tell us how to include this parameter? Thank you.
I'm an apprentice among journeymen who spend a lot of time on the virtual road.
Frank Slootweg
Honored Contributor
Solution

Re: tool to edit mailfile

It is probably best to *first* get rid of the 'leading' "U" and "N". That makes the second column (message number) more reliable.

Then you can squeeze multiple space to single ones with "tr -s ' '".

The resulting output can be cut(1) into *fields* ("-f...." and "-d' '") instead of columns, and grep(1)-ed on an empty (' $') subject field.

The following works for me, but may depend on the size (i.e. one, two, three, ... digits) of the message number:

$ mailx -H -f /tmp/franks | cut -c3- | tr -s ' ' | cut -f2,9 -d' ' | grep ' $'
7
$

The first cut(1) is the tricky part.

Also putting ' $' into the SUB variable is somewhat tricky.

This seems to work:

for SUB in ' $' ....

and

grep "$SUB"

Note the single quotes in the 'for' command (because the dollar must not be interpreted) and the double quotes around $SUB (because it can contain a space, but needs to be interpreted.

You have *two* 'for' loops, so the (non)quoting becomes more complex.



Raymond Ford
Advisor

Re: tool to edit mailfile

Frank - We're getting closer. I should have mentioned that we're using HP-UX 10.20. Your line $ mailx -H -f /tmp/franks | cut -c3- | tr -s ' ' | cut -f2,9 -d' ' | grep ' $' we had to slightly modify in order for the output to contain only message numbers. What worked is $ mailx -H -f /tmp/franks | cut -c2- | tr -s ' ' | cut -f2,9 -d' ' | grep '[0-9] $'

We're on our way though. We came up with the [0-9] after vi-ing the mailfile to look for nonprinted characters and such. Never knew it'd become such a monster. It's obvious you put a lot of thought into this. Thank-you.
I'm an apprentice among journeymen who spend a lot of time on the virtual road.