![如果条件不起作用,则 awk 如果使用 ||](https://linux22.com/image/163609/%E5%A6%82%E6%9E%9C%E6%9D%A1%E4%BB%B6%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8%EF%BC%8C%E5%88%99%20awk%20%E5%A6%82%E6%9E%9C%E4%BD%BF%E7%94%A8%20%7C%7C.png)
我正在寻找编写一个脚本来 grep 一个单词并比较错误的时间。如果时间不等于 01 或 04 那么它应该打印错误时间
grep -e "Socket disconnected" -e "App Server collections connection state[False]" log-2019-10* |
awk '{if ((substr($2,1,2) != "04")) print (substr($2,1,8));}'
15:00:03
19:02:44
19:02:44
21:27:48
21:27:48
20:20:13
01:49:25
01:49:54
但是当我使用
grep -e "Socket disconnected" -e "App Server collections connection state[False]" log-2019-10* |
awk '{if ((substr($2,1,2) != "01") || (substr($2,1,2) != "04")) print (substr($2,1,8))}'
它正在打印所有的计时
04:56:55
04:56:55
04:55:25
04:55:25
04:56:26
04:55:27
04:55:28
04:55:24
04:55:24
15:00:03
19:02:44
19:02:44
21:27:48
21:27:48
04:55:22
04:55:22
04:55:25
04:55:25
04:55:24
04:55:24
04:55:25
04:55:25
04:55:24
04:55:24
04:55:24
04:55:24
04:55:24
04:55:24
04:55:25
04:55:25
04:55:28
04:55:28
04:55:24
04:55:24
20:20:13
04:55:24
04:55:24
04:55:24
04:55:24
04:55:26
04:55:26
04:55:25
04:55:25
04:55:25
04:55:25
04:55:23
04:55:23
04:55:23
04:55:23
01:49:25
01:49:54
04:55:23
04:55:24
04:55:24
04:55:26
04:55:26
04:55:23
04:55:23
04:55:24
04:55:24
04:55:24
04:55:24
04:55:24
04:55:24
04:55:24
04:55:24
04:55:24
04:55:24
04:55:25
04:55:25
有人能帮我吗?
答案1
当您使用 时if ((substr($2,1,2) != "01") || (substr($2,1,2) != "04"))
,当时间不是“01”或时间不是“04”时,条件为真;这始终是正确的,包括“01”(不是“04”)和“04”(不是“01”)。
你应该使用&&
:
grep -e "Socket disconnected" -e "App Server collections connection state[False]" log-2019-10* |
awk '{if ((substr($2,1,2) != "01") && (substr($2,1,2) != "04")) print (substr($2,1,8))}'
产生
15:00:03
19:02:44
19:02:44
21:27:48
21:27:48
20:20:13
这是一个实例德摩根定律:您要检查时间不是“01”或“04”,IE !("01" || "04")
,相当于!"01" && !"04"
。
顺便说一句,您可以结合grep
在 AWK 中执行过滤:
awk '(/Socket disconnected/ || /App Server collections connection state\[False]/) && substr($2,1,2) != "01" && substr($2,1,2) != "04" { print substr($2,1,8) }' log-2019-10*