我需要创建一个bash脚本,当显示“找不到有效的许可证”消息时向我发送通知。
我的日志文件包含5年前的日志。因此,我需要此脚本仅读取今天的内容。(脚本将每天运行)。
到目前为止,我已经到了:
#! /usr/bin/bash
now=$(date)
while read LINE;
do
for WORD in $LINE;
do
if [ "$WORD" == "license" ]
then
echo "the word has been found"
fi
done
done <"text.txt"
如何仅读取以当前日期开头的行?另外,我需要搜索一个短语,而不是单个单词。
我的日志文件如下所示:
2020-12-08 23:55:38 INFO: [system] automatic save: begin
2020-12-08 23:55:39 INFO: [system] automatic save: end
2020-12-09 00:04:32 INFO: [system] user '_internal_suite' logged in
2020-12-09 00:04:32 INFO: [_internal_suite] user '_internal_suite' logged out
2020-12-09 00:04:52 INFO: [system] user 'admin' logged in
2020-12-09 00:04:55 WARNING: [system] error code: 12001 description: no valid license found message: No valid license found.
您可以通过AWK
以下方式使用GNU :
awk 'BEGIN{today=strftime("%Y-%m-%d")}(today==$1 && index($0,"No valid license found")){print}' file.txt
file.txt
文件名在哪里。说明:我today
以YYYY-mm-dd格式设置为当前日期,然后如果第一列是这一天并且No valid license found
整行中有某处,我print
说这行。
(在gawk 4.2.1中测试)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句