awk for loop for each line in a file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite
1












I need to figure out how to get loop through a text file and for every line take the fields of name , project #, and email, and replace them in a email template to be sent out.



So this is the text file called send.txt:



Project 1,Jack,Chen,06,12,test@cs.fiu.edu
Project 2,Emily,Weiss,06,12,eweiss@cs.fiu.edu
Project 3,Mary,Gonzalas,06,12,Mgonz@cs.fiu.edu


and this is the email template called Reminder.email:



Dear __FULLNAME__: 

This is a kindly reminder that our __Project__ meeting will be held on today.

Best Regards,
CIS5027


So for every line in the text file I need to replace in this email template the fields of FULLNAME: , and Project . With the corresponding values which I can do for the first line, however I cannot do it for every line.



This is my script



#

!/bin/sh
#Start your code from here

date= date "+%m/%d/%y"
print $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.

cat sendtoday.txt | sed 's/^..//' > send.txt


#This is where im having trouble. When storing the values for the variables below of name, email, project #. The value of NR==1 thus it never goes through the rest of the lines. I've tried different solutions but none seem to work.

p=$(awk -F ',' 'NR==1print $1' send.txt)
n=$(awk -F ',' 'NR==1print $2' send.txt)
l=$(awk -F ',' 'NR==1print $3' send.txt)
e=$(awk -F ',' 'NR==1print $6' send.txt)

echo $p $n $l $e

#This part is to replace the values in the email template using sed and save the modified template as sendnow.txt.

sed -e "s/__FULLNAME__:/ $n $l :/g;s/__Project__/ $p /g" Reminder.email > sendnow.txt

cat sendnow.txt


#Yet to be written ... send out modified email templates.
exit 0

########


This is the output it produces :



06/12/14
Project 1 Jack Chen test@cs.fiu.edu
Dear Jack Chen :

This is a kindly reminder that our Project 1 meeting will be held on today.

Best Regards,
CIS5027


As you see it did properly replace the fields but only for Jack Chen. There were 3 lines in the send.txt file so there must be 3 modified versions of the above template.










share|improve this question























  • stackoverflow.com/questions/15945302/… this seems to solve your problem.
    – Jasper
    Jun 12 '14 at 8:27














up vote
5
down vote

favorite
1












I need to figure out how to get loop through a text file and for every line take the fields of name , project #, and email, and replace them in a email template to be sent out.



So this is the text file called send.txt:



Project 1,Jack,Chen,06,12,test@cs.fiu.edu
Project 2,Emily,Weiss,06,12,eweiss@cs.fiu.edu
Project 3,Mary,Gonzalas,06,12,Mgonz@cs.fiu.edu


and this is the email template called Reminder.email:



Dear __FULLNAME__: 

This is a kindly reminder that our __Project__ meeting will be held on today.

Best Regards,
CIS5027


So for every line in the text file I need to replace in this email template the fields of FULLNAME: , and Project . With the corresponding values which I can do for the first line, however I cannot do it for every line.



This is my script



#

!/bin/sh
#Start your code from here

date= date "+%m/%d/%y"
print $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.

cat sendtoday.txt | sed 's/^..//' > send.txt


#This is where im having trouble. When storing the values for the variables below of name, email, project #. The value of NR==1 thus it never goes through the rest of the lines. I've tried different solutions but none seem to work.

p=$(awk -F ',' 'NR==1print $1' send.txt)
n=$(awk -F ',' 'NR==1print $2' send.txt)
l=$(awk -F ',' 'NR==1print $3' send.txt)
e=$(awk -F ',' 'NR==1print $6' send.txt)

echo $p $n $l $e

#This part is to replace the values in the email template using sed and save the modified template as sendnow.txt.

sed -e "s/__FULLNAME__:/ $n $l :/g;s/__Project__/ $p /g" Reminder.email > sendnow.txt

cat sendnow.txt


#Yet to be written ... send out modified email templates.
exit 0

########


This is the output it produces :



06/12/14
Project 1 Jack Chen test@cs.fiu.edu
Dear Jack Chen :

This is a kindly reminder that our Project 1 meeting will be held on today.

Best Regards,
CIS5027


As you see it did properly replace the fields but only for Jack Chen. There were 3 lines in the send.txt file so there must be 3 modified versions of the above template.










share|improve this question























  • stackoverflow.com/questions/15945302/… this seems to solve your problem.
    – Jasper
    Jun 12 '14 at 8:27












up vote
5
down vote

favorite
1









up vote
5
down vote

favorite
1






1





I need to figure out how to get loop through a text file and for every line take the fields of name , project #, and email, and replace them in a email template to be sent out.



So this is the text file called send.txt:



Project 1,Jack,Chen,06,12,test@cs.fiu.edu
Project 2,Emily,Weiss,06,12,eweiss@cs.fiu.edu
Project 3,Mary,Gonzalas,06,12,Mgonz@cs.fiu.edu


and this is the email template called Reminder.email:



Dear __FULLNAME__: 

This is a kindly reminder that our __Project__ meeting will be held on today.

Best Regards,
CIS5027


So for every line in the text file I need to replace in this email template the fields of FULLNAME: , and Project . With the corresponding values which I can do for the first line, however I cannot do it for every line.



This is my script



#

!/bin/sh
#Start your code from here

date= date "+%m/%d/%y"
print $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.

cat sendtoday.txt | sed 's/^..//' > send.txt


#This is where im having trouble. When storing the values for the variables below of name, email, project #. The value of NR==1 thus it never goes through the rest of the lines. I've tried different solutions but none seem to work.

p=$(awk -F ',' 'NR==1print $1' send.txt)
n=$(awk -F ',' 'NR==1print $2' send.txt)
l=$(awk -F ',' 'NR==1print $3' send.txt)
e=$(awk -F ',' 'NR==1print $6' send.txt)

echo $p $n $l $e

#This part is to replace the values in the email template using sed and save the modified template as sendnow.txt.

sed -e "s/__FULLNAME__:/ $n $l :/g;s/__Project__/ $p /g" Reminder.email > sendnow.txt

cat sendnow.txt


#Yet to be written ... send out modified email templates.
exit 0

########


This is the output it produces :



06/12/14
Project 1 Jack Chen test@cs.fiu.edu
Dear Jack Chen :

This is a kindly reminder that our Project 1 meeting will be held on today.

Best Regards,
CIS5027


As you see it did properly replace the fields but only for Jack Chen. There were 3 lines in the send.txt file so there must be 3 modified versions of the above template.










share|improve this question















I need to figure out how to get loop through a text file and for every line take the fields of name , project #, and email, and replace them in a email template to be sent out.



So this is the text file called send.txt:



Project 1,Jack,Chen,06,12,test@cs.fiu.edu
Project 2,Emily,Weiss,06,12,eweiss@cs.fiu.edu
Project 3,Mary,Gonzalas,06,12,Mgonz@cs.fiu.edu


and this is the email template called Reminder.email:



Dear __FULLNAME__: 

This is a kindly reminder that our __Project__ meeting will be held on today.

Best Regards,
CIS5027


So for every line in the text file I need to replace in this email template the fields of FULLNAME: , and Project . With the corresponding values which I can do for the first line, however I cannot do it for every line.



This is my script



#

!/bin/sh
#Start your code from here

date= date "+%m/%d/%y"
print $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.

cat sendtoday.txt | sed 's/^..//' > send.txt


#This is where im having trouble. When storing the values for the variables below of name, email, project #. The value of NR==1 thus it never goes through the rest of the lines. I've tried different solutions but none seem to work.

p=$(awk -F ',' 'NR==1print $1' send.txt)
n=$(awk -F ',' 'NR==1print $2' send.txt)
l=$(awk -F ',' 'NR==1print $3' send.txt)
e=$(awk -F ',' 'NR==1print $6' send.txt)

echo $p $n $l $e

#This part is to replace the values in the email template using sed and save the modified template as sendnow.txt.

sed -e "s/__FULLNAME__:/ $n $l :/g;s/__Project__/ $p /g" Reminder.email > sendnow.txt

cat sendnow.txt


#Yet to be written ... send out modified email templates.
exit 0

########


This is the output it produces :



06/12/14
Project 1 Jack Chen test@cs.fiu.edu
Dear Jack Chen :

This is a kindly reminder that our Project 1 meeting will be held on today.

Best Regards,
CIS5027


As you see it did properly replace the fields but only for Jack Chen. There were 3 lines in the send.txt file so there must be 3 modified versions of the above template.







shell-script sed awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 20:55









Rui F Ribeiro

38.2k1475123




38.2k1475123










asked Jun 12 '14 at 7:51









S0urc3

28114




28114











  • stackoverflow.com/questions/15945302/… this seems to solve your problem.
    – Jasper
    Jun 12 '14 at 8:27
















  • stackoverflow.com/questions/15945302/… this seems to solve your problem.
    – Jasper
    Jun 12 '14 at 8:27















stackoverflow.com/questions/15945302/… this seems to solve your problem.
– Jasper
Jun 12 '14 at 8:27




stackoverflow.com/questions/15945302/… this seems to solve your problem.
– Jasper
Jun 12 '14 at 8:27










3 Answers
3






active

oldest

votes

















up vote
5
down vote



accepted










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

########





share|improve this answer




















  • This works like clockwork. Thank you so much for your help. I think this is the most concise answer. I will be seeking your help again in the future :-) .
    – S0urc3
    Jun 12 '14 at 14:52

















up vote
3
down vote













You have used NR==1 condition NR==1print $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





share|improve this answer






















  • This is the approach I wanted to take but couldn't get this to work out. The above IFS approach works good though. Thanks for your input. Ill try to test this again.
    – S0urc3
    Jun 12 '14 at 14:54

















up vote
1
down vote













maybe wrap your script in something like



for i in $(cat send.txt); do echo "line: $i"; done


?






share|improve this answer




















  • -1. Very bad idea I'm afraid. 1) That reads the entire line (kind of, see the next point) into $i which does not help the OP. 2) You never want to do for i in $(cat file), you should always use while read i; do ... ; done < file instead. With the for loop, the line will be split on whitespace so $i will be Project, then 1,Jack,Chen,06,12,test@cs.fiu.edu for the first line. 3) This doesn't answer the question in any case since you don't address the actual issue the OP had.
    – terdon
    Jun 12 '14 at 8:51










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f136727%2fawk-for-loop-for-each-line-in-a-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote



accepted










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

########





share|improve this answer




















  • This works like clockwork. Thank you so much for your help. I think this is the most concise answer. I will be seeking your help again in the future :-) .
    – S0urc3
    Jun 12 '14 at 14:52














up vote
5
down vote



accepted










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

########





share|improve this answer




















  • This works like clockwork. Thank you so much for your help. I think this is the most concise answer. I will be seeking your help again in the future :-) .
    – S0urc3
    Jun 12 '14 at 14:52












up vote
5
down vote



accepted







up vote
5
down vote



accepted






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

########





share|improve this answer












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

########






share|improve this answer












share|improve this answer



share|improve this answer










answered Jun 12 '14 at 8:48









terdon

126k31241418




126k31241418











  • This works like clockwork. Thank you so much for your help. I think this is the most concise answer. I will be seeking your help again in the future :-) .
    – S0urc3
    Jun 12 '14 at 14:52
















  • This works like clockwork. Thank you so much for your help. I think this is the most concise answer. I will be seeking your help again in the future :-) .
    – S0urc3
    Jun 12 '14 at 14:52















This works like clockwork. Thank you so much for your help. I think this is the most concise answer. I will be seeking your help again in the future :-) .
– S0urc3
Jun 12 '14 at 14:52




This works like clockwork. Thank you so much for your help. I think this is the most concise answer. I will be seeking your help again in the future :-) .
– S0urc3
Jun 12 '14 at 14:52












up vote
3
down vote













You have used NR==1 condition NR==1print $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





share|improve this answer






















  • This is the approach I wanted to take but couldn't get this to work out. The above IFS approach works good though. Thanks for your input. Ill try to test this again.
    – S0urc3
    Jun 12 '14 at 14:54














up vote
3
down vote













You have used NR==1 condition NR==1print $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





share|improve this answer






















  • This is the approach I wanted to take but couldn't get this to work out. The above IFS approach works good though. Thanks for your input. Ill try to test this again.
    – S0urc3
    Jun 12 '14 at 14:54












up vote
3
down vote










up vote
3
down vote









You have used NR==1 condition NR==1print $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





share|improve this answer














You have used NR==1 condition NR==1print $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






share|improve this answer














share|improve this answer



share|improve this answer








edited Jun 12 '14 at 16:57









terdon

126k31241418




126k31241418










answered Jun 12 '14 at 8:57









Tingrammer

35528




35528











  • This is the approach I wanted to take but couldn't get this to work out. The above IFS approach works good though. Thanks for your input. Ill try to test this again.
    – S0urc3
    Jun 12 '14 at 14:54
















  • This is the approach I wanted to take but couldn't get this to work out. The above IFS approach works good though. Thanks for your input. Ill try to test this again.
    – S0urc3
    Jun 12 '14 at 14:54















This is the approach I wanted to take but couldn't get this to work out. The above IFS approach works good though. Thanks for your input. Ill try to test this again.
– S0urc3
Jun 12 '14 at 14:54




This is the approach I wanted to take but couldn't get this to work out. The above IFS approach works good though. Thanks for your input. Ill try to test this again.
– S0urc3
Jun 12 '14 at 14:54










up vote
1
down vote













maybe wrap your script in something like



for i in $(cat send.txt); do echo "line: $i"; done


?






share|improve this answer




















  • -1. Very bad idea I'm afraid. 1) That reads the entire line (kind of, see the next point) into $i which does not help the OP. 2) You never want to do for i in $(cat file), you should always use while read i; do ... ; done < file instead. With the for loop, the line will be split on whitespace so $i will be Project, then 1,Jack,Chen,06,12,test@cs.fiu.edu for the first line. 3) This doesn't answer the question in any case since you don't address the actual issue the OP had.
    – terdon
    Jun 12 '14 at 8:51














up vote
1
down vote













maybe wrap your script in something like



for i in $(cat send.txt); do echo "line: $i"; done


?






share|improve this answer




















  • -1. Very bad idea I'm afraid. 1) That reads the entire line (kind of, see the next point) into $i which does not help the OP. 2) You never want to do for i in $(cat file), you should always use while read i; do ... ; done < file instead. With the for loop, the line will be split on whitespace so $i will be Project, then 1,Jack,Chen,06,12,test@cs.fiu.edu for the first line. 3) This doesn't answer the question in any case since you don't address the actual issue the OP had.
    – terdon
    Jun 12 '14 at 8:51












up vote
1
down vote










up vote
1
down vote









maybe wrap your script in something like



for i in $(cat send.txt); do echo "line: $i"; done


?






share|improve this answer












maybe wrap your script in something like



for i in $(cat send.txt); do echo "line: $i"; done


?







share|improve this answer












share|improve this answer



share|improve this answer










answered Jun 12 '14 at 8:47









Andreas John

612




612











  • -1. Very bad idea I'm afraid. 1) That reads the entire line (kind of, see the next point) into $i which does not help the OP. 2) You never want to do for i in $(cat file), you should always use while read i; do ... ; done < file instead. With the for loop, the line will be split on whitespace so $i will be Project, then 1,Jack,Chen,06,12,test@cs.fiu.edu for the first line. 3) This doesn't answer the question in any case since you don't address the actual issue the OP had.
    – terdon
    Jun 12 '14 at 8:51
















  • -1. Very bad idea I'm afraid. 1) That reads the entire line (kind of, see the next point) into $i which does not help the OP. 2) You never want to do for i in $(cat file), you should always use while read i; do ... ; done < file instead. With the for loop, the line will be split on whitespace so $i will be Project, then 1,Jack,Chen,06,12,test@cs.fiu.edu for the first line. 3) This doesn't answer the question in any case since you don't address the actual issue the OP had.
    – terdon
    Jun 12 '14 at 8:51















-1. Very bad idea I'm afraid. 1) That reads the entire line (kind of, see the next point) into $i which does not help the OP. 2) You never want to do for i in $(cat file), you should always use while read i; do ... ; done < file instead. With the for loop, the line will be split on whitespace so $i will be Project, then 1,Jack,Chen,06,12,test@cs.fiu.edu for the first line. 3) This doesn't answer the question in any case since you don't address the actual issue the OP had.
– terdon
Jun 12 '14 at 8:51




-1. Very bad idea I'm afraid. 1) That reads the entire line (kind of, see the next point) into $i which does not help the OP. 2) You never want to do for i in $(cat file), you should always use while read i; do ... ; done < file instead. With the for loop, the line will be split on whitespace so $i will be Project, then 1,Jack,Chen,06,12,test@cs.fiu.edu for the first line. 3) This doesn't answer the question in any case since you don't address the actual issue the OP had.
– terdon
Jun 12 '14 at 8:51

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f136727%2fawk-for-loop-for-each-line-in-a-file%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay