在elb日志文件bash中获取超过5分钟的数据

时间:2017-08-17 09:51:33

标签: bash logging

当用户提示 10:07:30 时,我正在编写一个脚本来获取此日志的最后五分钟。我怎么能做这个bash? Give是我脚本的一部分。

2017-07-19T10:00:37.306805Z elb-sam30-ftpoc-prod 35.163.110.235:58814 10.0.2.203:80 0.000042 0.000579 0.00002 200 200 0 809 2017-07-19T10:01:56.572506Z elb-sam30-ftpoc-prod 35.163.110.235:58870 10.0.2.203:80 0.000038 0.000603 0.000019 200 200 0 809 2017-07-19T10:02:15.628179Z elb-sam30-ftpoc-prod 35.163.110.235:58904 10.0.2.203:80 0.000044 0.000583 0.00002 200 200 0 809 2017-07-19T10:03:34.875186Z elb-sam30-ftpoc-prod 35.163.110.235:58958 10.0.2.203:80 0.00004 0.000597 0.00002 200 200 0 809 2017-07-19T10:04:27.468370Z elb-sam30-ftpoc-prod 35.163.110.235:59400 10.0.2.203:80 0.000065 0.000513 0.000019 200 200 0 809 2017-07-19T10:05:46.601192Z elb-sam30-ftpoc-prod 35.163.110.235:59446 10.0.2.203:80 0.000057 0.000544 0.000021 200 200 0 809 2017-07-19T10:06:05.782121Z elb-sam30-ftpoc-prod 35.163.110.235:59492 10.0.2.203:80 0.000044 0.000667 0.00002 200 200 0 809 2017-07-19T10:07:24.983365Z elb-sam30-ftpoc-prod 35.163.110.235:59546 10.0.2.203:80 0.000041 0.000434 0.000019 200 200 0 809 2017-07-19T10:08:00.975682Z elb-sam30-ftpoc-prod 35.163.110.235:59794 10.0.2.203:80 0.000037 0.000521 0.000019 200 200 0 809 2017-07-19T10:09:20.152264Z elb-sam30-ftpoc-prod 35.163.110.235:59842 10.0.2.203:80 0.000045 0.000571 0.00002 200 200 0 809

1 个答案:

答案 0 :(得分:0)

 var storage = GetStorageAccount(resourceGroup, storageName);
 CloudFileClient fileClient = storage.CreateCloudFileClient();
 CloudFileShare share = fileClient.GetShareReference(projectId.ToString());
击败了awk:

dat="Thu Aug 17 10:07:11 IST 2017"
dat1=$(sed -rn 's/([[:alpha:]]{3}[[:blank:]][[:alpha:]]{3}[[:blank:]][[:digit:]]{2}[[:blank:]])(.*)(IST )(.*$)/\1\4 \2/p' <<< $dat)
edat=$(date -d "$dat1" +%s)
awk -v rdat=$edat '{ram1=substr($1,1,4)" "substr($1,6,2)" "substr($1,9,2)" "substr($1,12,2)" "substr($1,15,2)" "substr($1,18,2);ram1=mktime(ram1);ram=rdat-(60*5); if (ram1 >= ram) { print $0 } }' logfile

我们首先用sed解析日期,然后以我们可以传递到命令日期的格式来获取日期,以便我们获得日期格式(自纪元以来的秒数)。然后将此变量edat作为变量rdat传递给awk。此日期减去五分钟(60 * 5),然后将它们与日志文件中的日期进行比较,之后它们也将使用mktime函数转换为纪元。