我想檢查一個日志檔案是否有任何實體,其中兩個或多個連續行使用 bash 包含相同的文本。將指定文本。在比較中忽略第三個欄位之后的時間戳和任何其他文本。
即 grep...“錯誤”/tmp/file.txt
此檔案將匹配:
2020-01-01 05:05 text1
2020-01-01 05:07 error
2020-01-01 05:15 error
2020-01-01 05:25 error
2020-01-01 05:45 text2
這不會
2020-01-01 05:05 text1
2020-01-01 05:15 error
2020-01-01 05:25 text2
2020-01-01 05:45 error
2020-01-01 05:05 text3
使用 grep、sed 或 awk 有什么想法嗎?理想情況下,我希望退出值 0 表示匹配,1 表示不匹配。
uj5u.com熱心網友回復:
看起來uniq 可以滿足您的一切需求。
-d , --repeated
只列印重復的行,每組一個
-s , --skip-chars = N
避免比較前 N 個字符
所以這應該適合你:
uniq --skip-chars=17 -d /tmp/file.txt
在我的機器上測驗:
$ cat in.txt
2020-01-01 05:05 text1
2020-01-01 05:07 error
2020-01-01 05:15 error
2020-01-01 05:25 error
2020-01-01 05:45 text2
$ uniq --skip-chars=17 -d in.txt
2020-01-01 05:07 error
uj5u.com熱心網友回復:
一個在 awk 中測驗兩個或多個連續行,這對我來說意味著在兩個連續行之后立即退出:
$ awk -v s="word" '{ # search word as a parameter
if($3==p&&$3==s) # if third word is the same as from previous round
exit ec=1 # and the same as the search word, exit right away
else
p=$3 # else just store the last word for next round
}
END { # in the end
exit !ec # flip the error code and exit
}' file
測驗它:
$ awk -v s=error '{if($3==p&&$3==s)exit ec=1;else p=$3}END{exit !ec}' matching
$ echo $?
1
$ awk -v s=error '{if($3==p&&$3==s)exit ec=1;else p=$3}END{exit !ec}' nonmatching
$ echo $?
0
在上述資料示例中,僅考慮了第三個單詞(或空格分隔的欄位)。如果要查找比單詞長的字串,請考慮更改示例$3中的substr($0,n)位置n==18(日期時間部分之后的字串起點):
$ awk -v s=error '{
if(substr($0,18)==p&&substr($0,18)==s)
exit ec=1
else
p=substr($0,18)
}
END {
exit !ec
}' file
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/524811.html
上一篇:如何過濾和排序JQ提取的資料?
