2016-07-22 65 views
0

您好我想grep基於時間的文件的一些數據。所以如果時間匹配我想從文件中獲取所有特定的行到一個新的文件: 以下是我的文件格式:基於unix的時間grep數據

1 - - [22/Jul/2016:07:46:15 -0500] "POST abc HTTP/1.0" 200 679 93071 
1 - - [22/Jul/2016:07:46:16 -0500] "POST abc HTTP/1.0" 200 679 100994 
1 - - [22/Jul/2016:07:46:17 -0500] "POST abc HTTP/1.0" 200 679 102270 
1 - - [22/Jul/2016:07:46:18 -0500] "POST abc HTTP/1.0" 200 679 103008 
1 - - [22/Jul/2016:07:46:19 -0500] "POST abc HTTP/1.0" 200 679 109170 
1 - - [22/Jul/2016:07:46:20 -0500] "POST abc HTTP/1.0" 200 679 101149 
1 - - [22/Jul/2016:07:46:21 -0500] "POST abc HTTP/1.0" 200 679 24796 
1 - - [22/Jul/2016:07:46:22 -0500] "POST abc HTTP/1.0" 200 679 93071 
1 - - [22/Jul/2016:07:46:23 -0500] "POST abc HTTP/1.0" 200 679 100994 
1 - - [22/Jul/2016:07:46:24 -0500] "POST abc HTTP/1.0" 200 679 102270 
1 - - [22/Jul/2016:07:47:25 -0500] "POST abc HTTP/1.0" 200 679 103008 
1 - - [22/Jul/2016:07:47:19 -0500] "POST abc HTTP/1.0" 200 679 109170 
1 - - [22/Jul/2016:07:47:20 -0500] "POST abc HTTP/1.0" 200 679 101149 

現在我想給grep,其時間是7時46分15秒7時47分20秒之間

回答

1

awk來救援!

$ awk -v p="[22/Jul/2016:" '$4 >= p"07:46:15" && $4 <= p"07:47:20"' file 

1 - - [22/Jul/2016:07:46:15 -0500] "POST abc HTTP/1.0" 200 679 93071 
1 - - [22/Jul/2016:07:46:16 -0500] "POST abc HTTP/1.0" 200 679 100994 
1 - - [22/Jul/2016:07:46:17 -0500] "POST abc HTTP/1.0" 200 679 102270 
1 - - [22/Jul/2016:07:46:18 -0500] "POST abc HTTP/1.0" 200 679 103008 
1 - - [22/Jul/2016:07:46:19 -0500] "POST abc HTTP/1.0" 200 679 109170 
1 - - [22/Jul/2016:07:46:20 -0500] "POST abc HTTP/1.0" 200 679 101149 
1 - - [22/Jul/2016:07:46:21 -0500] "POST abc HTTP/1.0" 200 679 24796 
1 - - [22/Jul/2016:07:46:22 -0500] "POST abc HTTP/1.0" 200 679 93071 
1 - - [22/Jul/2016:07:46:23 -0500] "POST abc HTTP/1.0" 200 679 100994 
1 - - [22/Jul/2016:07:46:24 -0500] "POST abc HTTP/1.0" 200 679 102270 
1 - - [22/Jul/2016:07:47:19 -0500] "POST abc HTTP/1.0" 200 679 109170 
1 - - [22/Jul/2016:07:47:20 -0500] "POST abc HTTP/1.0" 200 679 101149 
2

只使用一個簡單的正則表達式的值:

grep ":07:46:1[5-9]" file 

T他的比賽從07:46:15到19:

了給定的輸入返回:

1 - - [22/Jul/2016:07:46:15 -0500] "POST abc HTTP/1.0" 200 679 93071 
1 - - [22/Jul/2016:07:46:16 -0500] "POST abc HTTP/1.0" 200 679 100994 
1 - - [22/Jul/2016:07:46:17 -0500] "POST abc HTTP/1.0" 200 679 102270 
1 - - [22/Jul/2016:07:46:18 -0500] "POST abc HTTP/1.0" 200 679 103008 
1 - - [22/Jul/2016:07:46:19 -0500] "POST abc HTTP/1.0" 200 679 109170 
1 - - [22/Jul/2016:07:46:15 -0500] "POST abc HTTP/1.0" 200 679 101149 
1 - - [22/Jul/2016:07:46:15 -0500] "POST abc HTTP/1.0" 200 679 24796 
+0

如果時間得到改變:07:46:15:08:12:13那麼它將無法工作 – Developer

+1

@Developer然後輪到你提供相關的數據,例如[MCVE。還請檢查[如何使用awk輕鬆過濾日誌?](http://stackoverflow.com/q/34311140/1983854),我在這裏介紹了這一點。 – fedorqui

+0

我們不能做這個grep嗎? – Developer