extract columns from file and send mail

Hi
I have a file of the form

name1,lastname1,email1@gmail.com,9.08243E+12,team1,role1,username1,password1
name2,lastname2,email2@gmail.com,9.08243E+11,team2,role2,username2,password2

I need to extract the email (column 3) and send a mail to each person, with their details ( specifically username and password.). so for eg 100 emails for 100 lines

I think this should be possible using awk and mailx but I am not able to get it to work

Any help would be appreciated.
Thanks

If you know, that email is always third, and username and password are always second to- and last ones, then this is a simple approach:

 awk -F, '{print "echo Your username is " $(NF-1) " and your password is " $NF " | mail -s mail_subject " $3}' file | sh 

If you omit the last pipe '| sh', you can test the output (print it on screen). To execute, pipe it to sh.

Hi mirni
How the the | sh work? would be glad if you could point me to some documentation
thanks

'sh' is shell (usually /bin/sh, which is usually a symbolic link to bash, dash, ksh or other shell).
So by piping a command to shell, it gets executed. Simple like that.

E.g.:

$ echo cat /etc/passwd
cat /etc/passwd
$ echo cat /etc/passwd | sh   #same as invoking 'cat /etc/passwd' directly
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
....

It is the same as calling the argument of echo in command line; that echoing makes debugging easy though, and will echo the command to the stdout before you are ready to execute it

1 Like