awk for loop for each line in a file
There is no reason to use awk
for this. You could do it directly in the shell using read
. The general format is read foo bar
which will save the first field as $foo
and the rest of each line as $bar
. So, in your case, you would do something like:
while IFS="," read p n l foo bar e; do
sed -e "s/__FULLNAME__:/\ $n $l :/g;s/__Project__/\ $p /g" Reminder.email;
done < file
The IFS
is the Input Field Separator which, when set to ,
reads comma delimited fields. This lets you take each field and store it in a variable. Note that I used two extra variables foo
and bar
. This is because each field needs its own variable name and you have 6 fields. If you only give 4 variable names, the 4th ($e
) will contain the fields 4 through last.
Now, there are various other syntax errors in your script. First of all the shebang line is wrong, you need #! /bin/sh
, there can't be a blank line between the #!
and the /bin/sh
. Also, in order to assign the output of a command to a variable, you need to use the var=`command`
or, preferably var=$(command)
format. Otherwise, the command itself as a string and not its output is assigned to the variable. Finally, print
is not what you think it is. You are looking for printf
or echo
. So, a better way to write your script would be:
#!/bin/sh
date=$(date "+%m/%d/%y")
echo $date
## The following line is to scan a file called Event-member.data
## and return any lines with todays date and save them to a file
## called sendtoday.txt
grep -n $(date +"%m,%d") Event-member.data > sendtoday.txt
## The following line is to remove the first to characters of the file
## created above sendtoday.txt and output that to a file called
## send.txt.
## I rewrote this to avoid the useless use of cat.
sed 's/^..//' sendtoday.txt > send.txt
## This is where you use read
while IFS="," read p n l foo bar e; do
sed -e "s/__FULLNAME__:/\ $n $l :/g;s/__Project__/\ $p /g" Reminder.email > sendnow.txt
cat sendnow.txt
## This is where you need to add the code that sends the emails. Something
## like this:
sendmail $e < sendnow.txt
done < send.txt
exit 0
########
You have used NR==1 condition NR==1{print $1}
. That means it will consider the first line of send.txt
. Use NR==2 condition to get for 2nd line and so on. OR use loop to go through all the lines like,
while read line
do
p=`echo $line | awk -F '.' '{print $1}'`
n=`echo $line | awk -F '.' '{print $2}'`
l=`echo $line | awk -F '.' '{print $3}'`
e=`echo $line | awk -F '.' '{print $1}'`
sed -e "s/\__FULLNAME\__:/\ $n $l :/g;s/\__Project__/\ $p /g" Reminder.email > sendnow.txt
done<send.txt