0

Hi i want to grep the some data from a file based on the time.So if the time matches i want to get all the particular row from the file into a new file: Below is my file format :

    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

Now i want to grep the values whose time is between 07:46:15 to 07:47:20

Developer
  • 6,292
  • 19
  • 55
  • 115

2 Answers2

2

Just use a simple regular expression:

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

This matches any time from 07:46:15 up to :19.

For your given input it returns:

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
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • if the time get changed :07:46:15 to :08:12:13 then it will not work – Developer Jul 22 '16 at 14:51
  • 1
    @Developer then it is your turn to provide relevant data, for example a [mcve]. And also check [How to filter logs easily with awk?](http://stackoverflow.com/q/34311140/1983854) where I covered this pretty much. – fedorqui Jul 22 '16 at 14:53
  • can't we do this grep ?? – Developer Jul 22 '16 at 15:11
  • @Developer `grep` can do this specific thing you asked in your question. For other things you may have in mind... it _depends_. As I said, show it to us. – fedorqui Jul 22 '16 at 15:17
  • i have updated the question please let guide me how we can get that data – Developer Jul 22 '16 at 15:34
1

awk to the rescue!

$ 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
karakfa
  • 66,216
  • 7
  • 41
  • 56