按照詳細資訊,該站點要求我包含一些文本,因為主要是代碼,所以我輸入了這句話,但我認為這是不言自明的
示例日志檔案:
jue 08 abr 2021 13:33:49 -03 : VA John Doe : PING google.com (17x.2xx.1x2.4x): 56 data bytes
--- google.com ping statistics ---
100 packets transmitted, 100 packets received, 0% packet loss
round-trip min/avg/max = 40.462/50.166/62.318 ms
jue 08 abr 2021 13:35:35 -03 : VA John Doe : PING google.com (17x.2xx.1x2.4x): 56 data bytes
--- google.com ping statistics ---
100 packets transmitted, 99 packets received, 1% packet loss
round-trip min/avg/max = 42.055/48.856/136.962 ms
jue 08 abr 2021 13:37:21 -03 : VA John Doe : PING google.com (17x.2xx.1x2.4x): 56 data bytes
--- google.com ping statistics ---
100 packets transmitted, 100 packets received, 0% packet loss
round-trip min/avg/max = 40.058/47.762/64.169 ms
到目前為止我的命令:
cat sample.log | sed -r -e '/^... [0-9] ... [0-9]{4} [0-9]{2}:[0-9]{2}/{s/(... [0-9] ... [0-9]{4} [0-9]{2}:[0-9]{2}).*$/\1/g;n;d}' -e '/^--- google.*$/d' -e 's/100 packets transmitted.*([0-9] %) packet.*$/\1/' -e '/round-trip/d'
獲得的結果:
jue 08 abr 2021 13:33
0%
jue 08 abr 2021 13:35
1%
jue 08 abr 2021 13:37
0%
理想的結果:
jue 08 abr 2021 13:33, 0%
jue 08 abr 2021 13:35, 1%
jue 08 abr 2021 13:37, 0%
uj5u.com熱心網友回復:
第一個解決方案:這應該是awk. 使用您顯示的示例,請嘗試以下awk代碼。
awk -v OFS=", " '
match($0,/^[a-zA-Z] [0-9]{2} [a-zA-Z] [0-9]{4} ([0-9]{2}:){2}[0-9]{2}/){
val=substr($0,RSTART,RLENGTH-3)
next
}
/packets transmitted/{
print val,$(NF-2)
val=""
}
' Input_file
說明:簡單的解釋是,使用match其中提到正則運算式進行匹配的函式^[a-zA-Z] [0-9]{2} [a-zA-Z] [0-9]{4} ([0-9]{2}:){2}[0-9]{2}(進一步解釋正則運算式),如果找到匹配項,則創建具有匹配值(捕獲值)的 val 變數正則運算式。Usingnext將從這里跳過所有進一步的陳述句。然后檢查條件是否行包含packets transmitted然后列印 val 以及該行的第三個最后一個欄位。val然后使變數無效。
正則運算式說明:
^[a-zA-Z] ##Matching small/capital letters 1 or more occurrences from starting.
[0-9]{2} ##Matching space followed by 2 occurrences of digits.
[a-zA-Z] ##Matching space followed by 2 occurrences of small/capital letters.
[0-9]{4} ##Matching space followed by followed by 4 digits.
([0-9]{2}:){2}[0-9]{2} ##Matching space followed by digits 2 occurrences followed by colon and this whole group should occur 2 times followed by 2 occurrences of digits.
第二種解決方案:awk在這里使用 GNU我們可以在 RS 變數中使用幾乎相同的提到的正則運算式,并且可以獲得如下所需的結果:
awk -v RS='[a-zA-Z] [0-9]{2} [a-zA-Z] [0-9]{4} [0-9]{2}:[0-9]{2}|[0-9]{1,3}%' -v OFS=", " '
RT{
val=(val?val ( count%2==0?ORS:OFS):"") RT
}
END{
print val
}
' Input_file
uj5u.com熱心網友回復:
要獲得所需的格式,您可以將輸出通過管道傳輸到:
sed 'N;s/\n/, /'
最后的命令變為(請注意,您不需要 cat 到 sed 因為它接受檔案名作為引數):
sed -r -e '/^... [0-9] ... [0-9]{4} [0-9]{2}:[0-9]{2}/{s/(... [0-9] ... [0-9]{4} [0-9]{2}:[0-9]{2}).*$/\1/g;n;d}' -e '/^--- google.*$/d' -e 's/100 packets transmitted.*([0-9] %) packet.*$/\1/' -e '/round-trip/d' sample.log | sed 'N;s/\n/, /'
輸出:
jue 08 abr 2021 13:33, 0%
jue 08 abr 2021 13:35, 1%
jue 08 abr 2021 13:37, 0%
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/372392.html
上一篇:決議提供給bash腳本的日期值
下一篇:GREP僅當整個字串
