我有一個具有以下格式的 apache 訪問日志,我正在嘗試使用 awk 命令過濾掉過去 15 分鐘內的請求。當有條目時它作業正常,但在過去 15 分鐘內沒有找到時回傳所有內容。
awk -vDate=`date -d'now-15 minute' [%d/%b/%Y:%H:%M:%S` ' { if ($4 > Date) print $9}' access.log
訪問日志格式
10.185.248.71 - - [09/Jan/2015:19:12:06 0000] 808840 "GET /inventoryService/inventory/purchaseItem?userId=20253471&itemId=23434300 HTTP/1.1" 500 17 "-" "Apache-HttpClient/4.2.6 (java 1.5)"
uj5u.com熱心網友回復:
無法直接在 bash 或 awk 中比較日期……但是您可以比較轉換為整數的日期……
#! /bin/bash
BEFORE=$(date -d 'now-15 minute' "%Y%m%d%H%M%S")
awk \
-v before="${BEFORE}" \
'
function toComparableDate (date) {
# 000000000111111111122
# 123456789012345678901
# [09/Jan/2015:19:12:06
return substr(date,9,4) hMonth[substr(date,5,3)] substr(date,2,2) substr(date,14,2) substr(date,17,2) substr(date,20,2)
}
BEGIN {
hMonth["Jan"] = "01"
hMonth["Feb"] = "02"
hMonth["Mar"] = "03"
hMonth["Apr"] = "04"
hMonth["May"] = "05"
hMonth["Jun"] = "06"
hMonth["Jul"] = "07"
hMonth["Aug"] = "08"
hMonth["Oct"] = "09"
hMonth["Sep"] = "10"
hMonth["Nov"] = "11"
hMonth["Dec"] = "12"
}
toComparableDate($4) > before {
print $8
}
' \
"$1"
像這樣執行:
./apachelogs.sh access.log
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364613.html
