当未输入日志中的确切时间戳时,如何在日期范围内过滤值
我想在给定的日期范围内从日志文件中获取价值计数。我的日志文件看起来像这样。
values.log
2022-01-01-10:01 AAA-passed
2022-01-01-11:05 AAA-passed
2022-01-01-12:01 AAA-passed
2022-01-01-13:05 AAA-passed
2022-01-02-12:01 AAA-failed
2022-01-03-13:05 AAA-failed
我已经尝试了以下方法将值计数在给定时间范围内。
t1='2022-01-01-10:01'
t2='2022-01-03-13:05'
pass=$(awk '/^'$t1.*'/,/'$t2.*'/' values.log | grep -w "AAA-passed" | wc -l)
echo $pass
仅当在日志文件中输入精确的时间戳时,此方法才能起作用。但是,如果我们给出未在日志文件中输入的时间范围,则此方法不起作用,也不给出答案,
例如,如果我们给出的
t1='2022-01-01-10:00'
t2='2022-01-03-14:00'
话,则没有给出任何答案,因为未输入T1和T2的这些确切值在日志文件中。我还尝试了许多其他方法,但对我没有任何帮助。有人可以帮我找出这一点吗?提前致谢..!
编辑 -
我为此找到了一个相关的答案,
awk -v 'start=2018-04-12 14:44:00.000' -v end='2018-04-12 14:45:00.000' '
/^[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2} / {
inrange = $0 >= start && $0 <= end
}
inrange' < your-file
此方法对我有用,但是我对T1和T2的代码值不硬,
t1=$(date -d "${dtd} -7 days" +'%Y-%m-%d-%R')
t2=$(date '+%Y-%m-%d-%R')
Result time format - 2022-07-05-12:15
Required time format - 2018-04-12 14:44:00.000
因此如何编辑上述表达式以获取所需时间格式的日期时间。
I want to takes value counts in a given date range from a log file. My log file is looks like this.
values.log
2022-01-01-10:01 AAA-passed
2022-01-01-11:05 AAA-passed
2022-01-01-12:01 AAA-passed
2022-01-01-13:05 AAA-passed
2022-01-02-12:01 AAA-failed
2022-01-03-13:05 AAA-failed
I have tried the following method to take the value counts in the given time range.
t1='2022-01-01-10:01'
t2='2022-01-03-13:05'
pass=$(awk '/^'$t1.*'/,/'$t2.*'/' values.log | grep -w "AAA-passed" | wc -l)
echo $pass
This method works only if exact timestamps have been entered in the log file. But if we give a time range which are not entered in the log file, this method does not work and not gives the answer,
for an example if we give
t1='2022-01-01-10:00'
t2='2022-01-03-14:00'
this not gives any answer, because these exact values for t1 and t2 are not entered in the log file. I tried lot of other methods also but nothing worked for me. Can someone help me to figure out this. Thanks in advance..!
Edit -
I found a relevant answer for this,
awk -v 'start=2018-04-12 14:44:00.000' -v end='2018-04-12 14:45:00.000' '
/^[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2} / {
inrange = $0 >= start && $0 <= end
}
inrange' < your-file
This method work for me, but I dont hard code values for t1 and t2
t1=$(date -d "${dtd} -7 days" +'%Y-%m-%d-%R')
t2=$(date '+%Y-%m-%d-%R')
Result time format - 2022-07-05-12:15
Required time format - 2018-04-12 14:44:00.000
so how can I edit the above expressions to get the date time in required time format.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@fravadona回答了您问的问题,因此您应该接受他们的答案,但这太长了,无法作为评论并需要格式化格式化。因此,这里是 - 仅供参考,除了您的时间戳比较外,您不需要管道时使用awk时的grep和wc:
@Fravadona answered the question you asked so you should accept their answer but this is too long to add as a comment and requires formatting so here it is - FYI in addition to your timestamp comparison, you don't need pipes to grep and wc when you're using awk:
您的时间戳格式为
yyyy-mm-dd-hh:mm
,因此您可以直接使用字符串比较:Your timestamp format is
YYYY-mm-dd-HH:MM
so you can directly use string comparisons: