简体   繁体   中英

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:

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.

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?

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. echo We are waiting for you at the next exam date from class. 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? 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   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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