如何从Unix发送电子邮件,其中10个文件中有3个文件的行数,大小和日期 [英] How to send an email from Unix with 3 files row count, size and date among 10 files

查看:31
本文介绍了如何从Unix发送电子邮件,其中10个文件中有3个文件的行数,大小和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是,我必须向用户发送一封电子邮件,其中必须包含目录中多个文件中3个文件的行数,大小和日期.文件将每个月生成一次,因此文件生成后必须每月发送一次邮件.

I'm in a requirement where i have to send an email to the users with 3 files row count, size and date among multiple files in a directory. Files will be generated every month so every month mail has to be sent after files generation.

目标目录:/工作/目录

目录下的文件列表:

month_f1.txt    
month_f2.txt    
month_f3.txt    
old_month_f1.txt    
old_month_f2.txt

电子邮件应按以下方式发送,并提供文件详细信息:

Email should be sent like below with files details:

Hi All,

PFB file details.    
---------------------------------
|filename       |rowcount |size  |  
---------------------------------
|month_f1.txt   |100      |20567 |  
|month_f2.txt   |200      |40567 |   
|month_f3.txt   |300      |60567 |
---------------------------------

提前谢谢!

推荐答案

这是经过重构的Awk脚本,可以一次性完成所有操作.

Here's a much refactored Awk script to do everything in one go.

awk 'BEGIN { print "Subject: Numbers for this month\n";
        print "Hi All,\n\nPFB file details\n"
        print "----------------------------------"
        print "|filename       |rowcount |size  |"
        print "----------------------------------" }
    { bytes[FILENAME] += length($0)+1; ++lines[FILENAME] }
    END { for (j=1; j<ARGC; ++j)
        printf("|%-15s|%9i|%6i|\n", ARGV[j], lines[ARGV[j]], bytes[ARGV[j]])
        print "----------------------------------" }' /work/directory/file* | 
sendmail email@domain.com

FILENAME 是内置的Awk变量,具有当前打开的文件的文件名,而 ARGV 是文件名参数的列表.其余的应该很明显.我们将此文件的字节数加上当前行的长度(对于终止的换行符再加上一个,由Awk删除),并增加行数.完成后,我们将打印累积的数字.

FILENAME is a built-in Awk variable with the file name of the currently open file, and ARGV is the list of file name arguments. The rest should be fairly obvious; we add the length of the current line (plus one for the terminating newline, which is stripped by Awk) to the byte count for this file, and increase the line count. When we are done, we print the accumulated numbers.

这篇关于如何从Unix发送电子邮件,其中10个文件中有3个文件的行数,大小和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆