gpt4 book ai didi

linux - 与时间戳相比,我可以 grep 获取结果吗?

转载 作者:太空狗 更新时间:2023-10-29 11:44:27 26 4
gpt4 key购买 nike

我有一个制表符分隔的文本文件,其中包含三个字段:TIMESTAMPHOSTSTATUS。我需要查找主机是否在不到一小时前被列为停机。到目前为止,我有这个例子:

grep "Down"thetextfile.txt | grep "主机名"

这给了我一个主机在日志中关闭的所有时间的小列表。凉爽的。现在我想我只需要了解最新的 TIMESTAMP 是否不到一小时前。我对 Linux 和 Bash 脚本编写还很陌生,但在我使用实际数据库的其他工作中,这将是一个相对简单的查询。

有什么想法吗?或者有更好的方法吗?

这是日志文件的示例:

TIMESTAMP       HOST    STATUS
Wed Oct 8 12:16:23 EDT 2014 aserver Alive
Wed Oct 8 12:16:23 EDT 2014 anotherserver Down

谢谢!

最佳答案

您可以使用这个 BASH 脚本:

#!/bin/bash

# current date-time in seconds (epoch) value
now=$(date '+%s')

while read -r p; do
# ignore 1st row with headers
[[ "$p" == *TIMESTAMP* ]] && continue

# read 3 values in 3 variables t h s
IFS=$'\t' && read t h s <<< "$p"

# convert date string to epoch value
ts=$(date -d "$t" '+%s')

# if date from file is less than 1 hour ago and status is Down then print host name
[[ "$s" == "Down" ]] && (( (now-ts) < 3600 )) && echo "$h"
done < file

关于linux - 与时间戳相比,我可以 grep 获取结果吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26406562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com