Skip to content

如何使用 shell script 篩選 nginx log 的時間區段

Published: at 04:02 AM (1 min read)

前陣子因為一些狀況,在試著查 nginx 的 access.log,於是剛好想說紀錄一下如何使用時間區間有效率的查 nginx log

OS: 不得不說 [awk](http://www.theunixschool.com/2011/05/awk-read-file-and-split-contents.html) 真的是方便又好用呢 ~


如果沒有特別更改過 nginx access.log 格式的話,應該會如下所示

[09/Jun/2020:16:33:25 107.23.45.127 - - [09/Jun/2020:18:13:37 +0800] "GET /robots.txt
awk -vDate=`date -d'now-2 hours' +[%d/%b/%Y:%H:%M:%S` '$4 > Date {print Date, $0}' access_log
awk -vDate=`date -d'now-4 hours' +[%d/%b/%Y:%H:%M:%S` -vDate2=`date -d'now-2 hours' +[%d/%b/%Y:%H:%M:%S` '$4 > Date && $4 < Date2 {print Date, Date2, $4} access_log'

參考資料:

https://stackoverflow.com/questions/7706095/filter-log-file-entries-based-on-date-range


Previous Post
常見的命名規則(Camel Case, Snake Case, Kebab Case)
Next Post
TypeError: this.getResolve is not a function