Reputation: 194
I have a text file. I need to select the address mail from each line and send each line to the correspond address.
I try to use foreach
and awk
to select the address
set valuea = `awk '{print $3}' FichierNote`
Could you please give me a way how can i do this? This is the data in the file.
8 mariamms [email protected] (cto;MDG_MMS)
7 lj16 [email protected] (gnb;DIG_FMTRD)
7 imbse [email protected] (gnb;MDG_MMS)
6 viviens [email protected] (gnb;IMD)
5 alberghv [email protected] (cto;ADG)
Upvotes: 0
Views: 68
Reputation: 194
Here's the script I ended up using, with csh
:
#!/bin/csh
foreach line ( "`cat FichierNote`" )
echo $line | mail -s "Subject" `echo $line | awk '{print $3}'`
end
Upvotes: 1
Reputation: 5591
You need to create a script like below:-
#!/usr/bin/csh
while read -r line
do
mailId=$(echo "$line" | awk '{print $3}')
#echo $mailId
#here you have to put your mail command
#example 'sendmail -f source_mail_id.com $mailId < mailContentFile.txt'
done < FichierNote
How will the above script work? In while
loop it will read each line and awk
command will cut the mail id each time and will store in mailId
variable. Now you need to put your mail command and put $mailId
variable as email address.
Upvotes: 1
Reputation: 8103
May be there is an simpler way, but this is what I could think of.
while read -r line; do echo "$line"|mail -s "your_subject" "$(echo "$line"|awk '{print $3}')"; done < emailList.txt
Replace your_subject
with the main subject.
Assumption: The email is at 3rd column.
Explanation:
while read -r line;do...done < file
will read the file line by line
and each line will be stored in variable $line
. "$(echo"$line"|awk '{print $3}')"
will get the email address to pass to
mail
command.
do echo "$line"|mail -s "your_subject" "$(echo "$line"|awk '{print
$3}')";
will send the line as message body to the email address
derived in above step.
Upvotes: 1