Parse Unix Mail


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse Unix Mail
# 1  
Old 01-17-2007
Parse Unix Mail

Hi,

This is my first thread on this forum.

I have an urgent requirement where I have to write a shell script to
- scan through the mails one by one,
- pick up the time when that mail is received,
- parse through the contents of that mail and pick up relevant information and load it into the database, and finally
- copy this mail to some other location.

The time the mail in received in obtained from the 1st line

for e.g.
From abc@xyz.com Mon Jan 15 11:18:18 2007

I need to pick up the time - "Mon Jan 15 11:18:18 2007"

After parsing through the contents (subject of the mail), which I think I can manage, I need to copy the mail into a different location, so that this mail is not processed once again.

This process would need to run every night.

Can this be done using shell scripts ??

Alternately, is there any way the mails can be split into individual mails and copied into separate files? I would need the header information in this as well as I need to pick the timestamp of the mail-received time.

Any help would be appreciated. I am short of time.

Many Thanks.

Ashish
# 2  
Old 01-17-2007
Quote:
Alternately, is there any way the mails can be split into individual mails and copied into separate files?
You are on unix therefore everything is possible Smilie ... well not really but have a look here
# 3  
Old 01-17-2007
Many Thanks for the response Andryk.

A few questions ...
I had worked with a sample of 3 mails.

1) I am not sure is `seq150` works in Unix. So I had used
for i in 1 2 3 instead of for i `seq 3`in the 1st line of your script.
2) In your script, in line no - 3,
echo s $i file.$i >> forpipe, I am not sure why was $i also echoed. For e.g. if there are 3 mails, then the file "forpipe" would look
like
s 1 file.1
s 2 file.2
s 3 file.3


Now when I had run your script, I got the following files created - file.1, fil2.2,file.3,1,2,3.

When I removed $1 from the script (line no 3), I got 3 files - file.1,file.2 and file.3.

3) The script runs fine if the number of mails = contents in the file forpipe.
for e.g.
the contents in the file file - forpipe is
s file.1
s file.2
s file.3
x

The number of mails = 3.

However when the number of mails = 4, and there are 3 files listed in forpilpe, the files are still created, but the mail 1-3 are not deleted. Am not sure why. Smilie

In my case, the mails can come any time, and a new mail could have come after the creation of forpipe, while the mails are being processed. I cannot process the email again, because each email is expected to be unique.

Is there a way out ?

Apologies for being so lengthly ... I hope I am making sense.

Many Thanks.

Last edited by its-ashish; 01-17-2007 at 08:37 AM..
# 4  
Old 01-17-2007
My apologize, that script was for linux when i was asked to resubmit someone's mail to his new office ...
I just wanted to show that You can split a unix mailbox provided You know the number of mail in the mailbox
Code:
let i=0
while [ $i -lt $number_of_mails ]
do
        let i=$i+1
        echo  s $i file.$i >> forstdin
       # Add more cmds here if needed...
done

echo x >> forstdin
# redirect standard input for the 'mail' utility
mail -f /var/spool/mail/usr_mbox < fortsdin

For the line that says 'echo s $i file.$i >> forstdin', it just relies on linux's mail utility that says to store mail at index n to file.n (s n file.n) no typo there Smilie, BTW You are on which unix flavor ?

Just copy one mailbox to one directory and try it there, it will split up the mailbox to file.1 file.2 ..., if You need to delete mail just dive into mail's man page You will not get lost, thats for sure Smilie
# 5  
Old 01-18-2007
Thanks for the modified script.

I am using Unix Korn shell on OS 5.8.

I am not sure if I have explained clearly.

Assuming there are 3 mails, and I use 'echo s $i file.$i >> forstdin', I get 6 files instead of 3. The files created are file.1,file.2,file.3,1,2,3. However when I use 'echo s file.$i >> forstdin', 3 files are created - file.1, file.2 and file.3, which seems okay.

The user command 's' save the mail into a file and deletes the mail, which is fine. However, as I had mentioned below the mails are not deleted when the number of files mentioned in 'fortsdin' is less than the number of mails.
I guess this has to do with the way unix works.

Thanks,
Ashish
This User Gave Thanks to its-ashish For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How can I send a mail from my outlook or other mail accounts to UNIX server?

Hi all, I want to send a mail for my business needs from outlook account to an unix server (HP-UX) but I don't send any mail. While I can send from the unix server to my outlook account, I can't send from outlook to unix. How can I achieve this ? How can I send a mail from my outlook or other... (2 Replies)
Discussion started by: igelegin
2 Replies

2. UNIX for Dummies Questions & Answers

How can i parse my Unix log files??

Hello, i would like to parse Unix log files and i would like to use a Unix syslog analyzer. I'm going to use Eucalyptus and i would like to parse its log files. Is there any open source/free syslog parser?? Thanks, in advance! (2 Replies)
Discussion started by: g_p
2 Replies

3. UNIX for Advanced & Expert Users

How to relay mail to mail server from Unix ?

Hi, I need to send email notifications from Unix/Linux box to users using mailx. In these Unix/Linux boxes mail is not configured however we are having mail server configured in our LAN. Could you please provide instructions to configure relay in these boxes so that we can relay our mails... (3 Replies)
Discussion started by: sourabhsharma
3 Replies

4. UNIX for Dummies Questions & Answers

Parse /var/mail

Hi all... We have a box that receives a lot of incoming emails. I have a .procmailrc file that in turn invokes a python script, which process each of these incoming email. All is nice and good. However, before the .procmailrc and python script were in place, we still were receiving emails and... (0 Replies)
Discussion started by: khader69
0 Replies

5. Shell Programming and Scripting

how to parse this file in unix

Hi all, I need to parse a file which is having this format: Details: 1 Name{ first=james second=steven } Sibling{ first=2 second=3 } Age{ first=13 second=14 } Friend{ jessy } Details: 2 (8 Replies)
Discussion started by: natalie23
8 Replies

6. UNIX for Dummies Questions & Answers

Unix mail to personal mail, Strange behavour

Hi. I am running a script on solaris 10 and mailing it to my personal email. the script is ran as user xxx, And i have edited the crontab for user xxx to run the script and mail it to my email account. /network_check.sh | /usr/bin/mailx -s "Network Test" k454@hotmail.com the sript... (2 Replies)
Discussion started by: k00061804
2 Replies

7. Shell Programming and Scripting

Mail attachment with unix mail

Hi Could someone help me with the details on how to send an attachment through mail in unix. I have an html file in my unix machine and I want this to be send to some mail id, plese help with the steps. Regards Ajay (2 Replies)
Discussion started by: ajaykumarboyana
2 Replies

8. Shell Programming and Scripting

Unix Script to parse a CSV

I am writing a unix script that will parse a CSV and edit the values. My CSV looks like this 0,0,0,0,1,0,1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0 10,11,7,0,4,12,2,3,7,0,11,3,12,4,0,5,5,4,5,0,8,6,12,0,9,3,3,0,2,7,8... (16 Replies)
Discussion started by: RJ17
16 Replies

9. UNIX for Dummies Questions & Answers

I am not able to send mail form unix to other mail accounts.

Using Mailx command i.e mailx -s "subject" chinni@hotmail.com < \tmp\chin this command executed sucessfully but not able to receive the mail in chinni@hotmail.com please help. (1 Reply)
Discussion started by: chinnigd
1 Replies

10. UNIX for Dummies Questions & Answers

can not send mail from unix server to company/yahoo mail

hi, Gurus, I need some help with sending mail out from my UNIX server: It is running Solaris 2.6 and the sendmail version is 8.8. Output of :/usr/lib/sendmail -d0.1 -bt < /dev/null Version 8.8.8+Sun Compiled with: LOG MATCHGECOS MIME7TO8 MIME8TO7 NAMED_BIND NDBM NETINET ... (5 Replies)
Discussion started by: b5fnpct
5 Replies
Login or Register to Ask a Question