My mails are stored in /var/mail/ file.
I want to write a bash script which will loopfor each message. I do not know how to split messages from a file.
Thanks
Edit : typical case of "When all you have is an hammer, everything looks like a nail" ; check formail
from the procmail mail-processing-package as suggested by twalberg.
For something quick and dirty, you should be able to use the following to separate the records with NUL bytes which would make iterating over them easier :
sed '/^\n/N;s/^From/\x0&/' /var/mail/targetMailbox
For example you can use this command and split
to split your mailbox into multiple files of manageable size :
sed '/^\n/N;s/^From/\x0&/' /var/mail/targetMailbox | split -l 100 -t'\0' - /tmp/mailbox
This command will split the mailbox into chunks of 100 messages which will be written to their own file in /tmp/
; check split
's options if you're interested in splitting the file, it supports a lot of different ways to do so.
A lot of (recent?) GNU tools will have a -0
or -z
option to make them handle NUL
-separated records, for example :
-z
for grep
, head
and tail
-0
for xargs
To iterate over them directly from bash
, the easiest is to use a while read
loop with read
's -d
option to specify the use of NUL as a separator.
For a more permanent solution, you need to find how to use an existing mbox
parser.