Send mail based on a group

Hello Gurus,
I have a question.

I have a file which has 2 columns and is tab separated and looks like below:

FIELD1 FIELD2

Manual m456@hotmail.com
Execution din098@gmail.com 
Artistic m456@hotmail.com
Graphic din098@gmail.com

FIELD2 values will have always either of the 2 above mentioned email addresses.What i'm trying to do is sort the FIELD2 values according to the email addresses i.e. which would look like below.

FIELD1 FIELD2

Manual m456@hotmail.com
Artistic m456@hotmail.com
Execution din098@gmail.com
Graphic din098@gmail.com

Now i would like to send only 1 email to the email address 'm456@hotmail.com' saying that you have two values assigned to you namely (Manual and artistic) and i would even want to send only 1 email to 'din098@gmail.com' saying that you have two values assigned to you namely (Execution and Graphic).

Could anybody please help me achieve the above requirement ?
Any help is highly appreciated.

awk 'NR>1 { print $2 | "sort | uniq" } ' input_file | while read mail_id
do
        awk ' BEGIN { print "Assigned Values: " } /'$mail_id'/ { print $1 } ' input_file | mailx -s "Group Info" ${mail_id}
done
1 Like
awk '{a[$NF]=a[$NF] FS $1}END{for (i in a) print "Assigned Values: " ,a |mailx -s "Group info" i}' infile
1 Like

Thank you very much gurus bipinajith and rdcwayx, i really appreciate your quick help.:smiley:

Thanks very much.

Hi bipinajith,

Actually the code would miss the first line and it's reading from the second line.
For instance if i have only one row of data, then there's no email sent to anybody.
I've tried removing NR>1 but it still does the same.
Can somebody help me with it please.

I put NR>1 to avoid the file header FIELD1 FIELD2

So removing NR>1 should work:

awk '{ print $2 | "sort | uniq" } ' input_file

Can you explain what exactly is the issue that you are facing?

Thanks for replying back.

The file does not have header fields.
Suppose if the file has only 1 row like following:

Manual m456@hotmail.com

then it's not sending mail to that id. ( i believe it's skipping first row).
I tried removing NR>1, but still it does not work.

But if the file has more than 1 rows like following:

Execution din098@gmail.com
Artistic din098@gmail.com
Graphic din098@gmail.com
Manual m456@hotmail.com

then it would send mail to both the id's (m456@hotmail.com and din098@gmail.com ).

Could you please help me with it. Your help is highly appreciated.

Works for me!

Here is the data in file: input_file

$ cat input_file
Manual m456@hotmail.com

Here I am running awk commenting out the mailing part:

$ awk '{ print $2 | "sort | uniq" } ' input_file | while read mail_id
do
        awk ' BEGIN { print "Assigned Values: " } /'$mail_id'/ { print $1 } ' input_file ###| mailx -s "Group Info" ${mail_id}
done
Assigned Values:
Manual

Note: Use nawk for Solaris and SunOS