简体   繁体   English

如何在 awk 命令内将文本打印到文件?

[英]How to print text to file inside the awk command?

I have a file containing information of users of a server: First name, last name, username, grade.我有一个包含服务器用户信息的文件:名字、姓氏、用户名、等级。

I have to send an email to each user containing this information, for example:我必须向每个包含此信息的用户发送 email,例如:

Dear First name, Last name!亲爱的名字,姓氏!

Your grade from 'class' is 'grade.您在“班级”中的成绩是“成绩”。

If the grade is below 5: We are waiting for you at the next exam date.如果分数低于 5:我们在下一个考试日期等你。

Also, I have to print these messages into one file, including the system time.另外,我必须将这些消息打印到一个文件中,包括系统时间。

I was able to generate these messages and put them together in one file.我能够生成这些消息并将它们放在一个文件中。 But how can I send each different email to the corresponding users?但是如何将每个不同的 email 发送给相应的用户?

The method which I am trying is printing the message in a file (whish I reuse again at each user), and then add the content to the main textfile.我正在尝试的方法是在文件中打印消息(我在每个用户处再次重用),然后将内容添加到主文本文件中。

But I get a message error at the second case:但是在第二种情况下我收到一条消息错误:

awk: cmd. line:17: 

This line is: Dear $1 $2.这一行是:亲爱的 1 美元 2 美元。 echo We are waiting for you at the next exam date from class. echo 我们在下一个考试日期等你上课。 echo回声

and I don't know what should I do.我不知道该怎么办。

Here is my code:这是我的代码:

awk -v class=$1 -v file=$2 '{   
if ($4 >= 5){      
        cat > message
        Dear $1 $2! echo Your class grade is $4. echo   
}
else {
        cat > message   
        Dear $1 $2! echo We are waiting for you at the next exam date from class. echo 
}
fi
}' $2 >$1_log                                                                                                                                                                                                                          

I can send a message with the following command:我可以使用以下命令发送消息:

mail -s "message" user

How can I generate these emails, send them to the users and print all of them together in one file?如何生成这些电子邮件,将它们发送给用户并将它们全部打印在一个文件中?

this should create the log file you're after.这应该创建您所追求的日志文件。 To send emails you need to have access to email addresses as well, perhaps in the same class file?要发送电子邮件,您还需要访问 email 地址,也许在同一个 class 文件中? Derived from username?从用户名派生?

$ awk '{print "Dear " $1 " " $2 "! Your class grade is " $4 "."
        if($4 < 5) 
           print "We are waiting for you at the next exam date from class"}}' class_file > log_file   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM