Group the records based on empno and send mail

Hi All,

I have records in a file with name,SSO,openitems,manageremail with | delimited file.Now i want to group the records by SSO and openitems and send email to their manageremail.

kiran|1111|draft|aaa@gmail.com
guna|2222|reject|bbb@gmail.com
kiran|1111|submitter|aaa@gmail.com
guna|2222|inclusion|bbb@gmail.com
jana|3333|reject|ccc@gmail.com

currently i am reading line by line from the file and sending 5 emails to their managers . but i want to group by SSO and openitems and redirect to sample.csv, attach the same file and send mail to his manager(i.e 4 th column).Please let me know how can i achieve this.

Thanks,
Kiran

You could sort the file by openitems and send the email when the openitems or email field changes:

$ cat t
kiran|1111|draft|aaa@gmail.com
guna|2222|reject|bbb@gmail.com
kiran|1111|submitter|aaa@gmail.com
guna|2222|inclusion|bbb@gmail.com
jana|3333|reject|ccc@gmail.com

$ sort -t'|' -k2 -k4 t
kiran|1111|draft|aaa@gmail.com
kiran|1111|submitter|aaa@gmail.com
guna|2222|inclusion|bbb@gmail.com
guna|2222|reject|bbb@gmail.com
jana|3333|reject|ccc@gmail.com

If I got your requirement right, you want to send one single mail only to each manager, containing all and only his projects. Try this:

$ sort -t'|' -k4 -k2,2 file | awk -F'|' '{print | "mail " $4}'

Not sure if you need e.g. a minus sign to make mail read stdin (the pipe).

I expect this to reopen the pipe whenever $4 is changing; if $4 stays the same, all lines should go into that single mail.