嗨,我想根据时间从文件中删除一些数据。因此,如果时间匹配,我想将文件中的所有特定行放入一个新文件:以下是我的文件格式:
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
现在我想对时间在07:46:15到07:47:20之间的值进行grep
只需使用一个简单的正则表达式:
grep ":07:46:1[5-9]" file
这与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
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