S0u*_*rc3 7 sed awk shell-script
我需要弄清楚如何遍历文本文件,并为每一行获取 name 、 project # 和 email 字段,并将它们替换为要发送的电子邮件模板。
所以这是名为 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
Run Code Online (Sandbox Code Playgroud)
这是名为 Reminder.email 的电子邮件模板:
Dear __FULLNAME__:
This is a kindly reminder that our __Project__ meeting will be held on today.
Best Regards,
CIS5027
Run Code Online (Sandbox Code Playgroud)
因此,对于文本文件中的每一行,我需要在此电子邮件模板中替换FULLNAME : 和Project的字段。使用我可以为第一行做的相应值,但是我不能为每一行都做。
这是我的脚本
#
!/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==1{print $1}' send.txt)
n=$(awk -F ',' 'NR==1{print $2}' send.txt)
l=$(awk -F ',' 'NR==1{print $3}' send.txt)
e=$(awk -F ',' 'NR==1{print $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
########
Run Code Online (Sandbox Code Playgroud)
这是它产生的输出:
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
Run Code Online (Sandbox Code Playgroud)
如您所见,它确实正确替换了字段,但仅适用于 Jack Chen。send.txt 文件中有 3 行,因此必须有上述模板的 3 个修改版本。
没有理由为此使用awk
。您可以直接在 shell 中使用read
. 一般格式是read foo bar
将第一个字段另存为,将$foo
每行的其余部分另存为$bar
. 因此,在您的情况下,您会执行以下操作:
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
Run Code Online (Sandbox Code Playgroud)
这IFS
是输入字段分隔符,当设置为,
读取逗号分隔的字段时。这使您可以获取每个字段并将其存储在变量中。请注意,我使用了两个额外的变量foo
和bar
. 这是因为每个字段都需要自己的变量名称,而您有 6 个字段。如果您只给出 4 个变量名称,则第 4 个 ( $e
) 将包含字段 4 到最后一个。
现在,您的脚本中还有其他各种语法错误。首先shebang行是错误的,你需要#! /bin/sh
,#!
和之间不能有空行/bin/sh
。此外,为了将命令的输出分配给变量,您需要使用var=`command`
or,最好是var=$(command)
格式。否则,将命令本身作为字符串而不是其输出分配给变量。最后,print
不是你想的那样。您正在寻找printf
或echo
。因此,编写脚本的更好方法是:
#!/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
########
Run Code Online (Sandbox Code Playgroud)