Using awk to read a file and process

I am fairly green using awk so I don't have anything started but what I am trying to do is: I have a file called "contacts" and in the file are 3 fields separate by ;. I want to read each line and send an email script. I have the email function working but just need to know how to setup awk to read each line and process. This is the sample of the contact file, it will grow so it will eventually have more than 3 lines :slight_smile:

8535;abccompany@yahoo.com;Dan
4200;123bakerbest@gmail.com;Tom
4300;daddy@godaddy.com;Jerry

Here is what I know how to do but not sure how to do a do while loop or for next loop with awk

counter=`wc -l "contacts" |awk '{print $1'}`
echo $counter
awk -F';' '{ print $1 $2 $3 }' contacts
echo "test"

Results give me the count of how many lines using wc with awk

3
8535abccompany@yahoo.comDan
4200123bakerbest@gmail.comTom
4300daddy@godaddy.comJerry
test

So what exactly do you want to do with which of the fields of each line?

My goal is to read one line of the contacts and then send an email using $2 as the send to email address and a file attached to the email that would be unique for that email address. I know how to arrange the email script I just don't know how to write the awk script to read each line then send the email then read the next line and send an email until the end of the file. thanks

Don't use awk for that; use simple shell:

while IFS=";" read var1 addr name; do mail -a file -s "Subject" $addr; ...; done < contacts
1 Like

I tried to adapt that code you suggested but it continues to fail. my email script requires the \ slash to go at the end of each line and I think that is creating problems. The other variables you see below, $mr and $yr are passed from previous prompts in the script.

while IFS=";" read an email to; do
          exec /u/star/sendEmail \ ;
             -f username@baby.net \ ;
             -t $email   \ ;
             -s smtp.windstream.net:587 \ ;
             -xu username     \  ;
             -xp password   \     ;
             -u "Monthly Statement" \ ;
             -m "$to, Please see attached account Statement" \ ;
             -a /u/star/data/20$yr/$mr/$an$mr$yr.pdf ;
echo $email $to $an $mr $yr >> /u/tmp/my.log; done < contacts

The above script results in nothing being sent and the email program giving an error about missing parameters. I have used the above email script without the ; separating each line and it works without the while statement added. Not sure how to continue each line but I believe that is the problem.

DON'T use exec - it will replace your current shell and you're ending up in nowhereland.

Are you sure you need that backslash (normally the "escape" char")? Are you sure you need that semicolon (shell's command end char)? Usual *nix syntax is to put all -options in a row, for a very long line you can use an "escaped <newline>" as a continuation char.
Try (without exec!) :

/u/star/sendmail -f ... -t $email -s ... -xu ...\
-xp ... -u ...

What be your system (OS, shell)?

I am using SCO 5.0.7 and using a bourne shell
Thanks

I have the script some what working. It will email the first statement but the next 2 it does not send but it includes them in the body of the first email on the first statement.

while IFS=";" read an addr name; do /u/star/sendEmail -f username@baby.net -t dzwic
k@ctc.net -s smtp.windstream.net:587 -xu username -xp password -u "statement" -a
/u/star/data/2013/04/"$an"0413.pdf
  done < contacts

sample of the contacts list

3810;username@baby.net;Dan
3860;username@baby.net;Tom
4001;username@baby.net;Jerry

That's because the mail programs read the "mail body" from stdin which points to the remainder of contacts and thus eats away the two lines. Redirect mail's stdin to a standard text (or to e.g. /dev/null) and try again...