2

I have my input like below -

[11/Jul/2016:13:40:43 +0000] "POST --" 200 - 7
[11/Jul/2016:13:40:43 +0000] "POST --" 200 - 7
[11/Jul/2016:13:40:47 +0000] "POST --" 200 - 7
[11/Jul/2016:13:40:47 +0000] "POST --" 500 - 7
[11/Jul/2016:13:41:48 +0000] "POST --" 200 - 7
[11/Jul/2016:13:41:49 +0000] "POST --" 500 - 7
[11/Jul/2016:13:42:12 +0000] "POST --" 500 - 7

I want the number of lines between the input 13:40 to 13:41 , in this case a count as 6.

I tried below options

echo sed -n '/^START_TIME/,/^END_TIME/ p' somelogfile.log | wc -l

echo sed -n '/^13:40:43$/,/^13:41:43$/ p' somelogfile.log | wc -l

but I get 1 all the time.

Can someone check and let me know what is going wrong here ?

  • The i/p lines got truncated . So in this case o/p should be 3. – user179309 Jul 12 '16 at 8:10
  • Is the ending timestamp (13:41:43) not present in the line on purpose, i.e. do you need a numerical comparison for the timestamps? – ilkkachu Jul 12 '16 at 8:27
3

The echo is going to generate only one line and wc is counting this line.

Note that the ending timestamp does not exist 13:41:43, and the anchors used (^ and $) are too restrictive.

Remove the echo and try this:

sed -n '/13:40:43/,/13:41:49/p' somelogfile.log | wc -l
  • Thank you very much for the quick reply. This is working perfect. – user179309 Jul 12 '16 at 8:47
  • This is my first post here and I am not very sure how it works. But I am unable accept your post as an answer. But this works. – user179309 Jul 12 '16 at 8:49
  • Then explain the reasons why you could not accept it? – Jay jargot Jul 12 '16 at 8:51
  • @user179309, I think you tried to accept more than one answer. You can vote up all you feel useful, but only accept one. Take a peek at the tour and the help pages. – ilkkachu Jul 12 '16 at 9:37
1

If the chronological order is the same as the lexical order you can just do:

awk '$0>="[11/Jul/2016:13:40:00" && $0<="[11/Jul/2016:13:41:59"' file | wc -l

This checks if the value is greater or equal to the first date and less or equal to the second date. If in awk no command is given, awk just prints the line.

  • As long as the start and end dates are on the same day, that should work. (Probably the best that can be easily done with that horrible timestamp format...) – ilkkachu Jul 12 '16 at 8:48
1

Let's review this command:

echo sed -n '/^13:40:43$/,/^13:41:43$/ p' somelogfile.log | wc -l
  • Consider what the echo does and what goes through the pipe to wc (just try without wc). That explains why you always get a 1.

  • The regexes you've written are locked to the beginning ^ and end $ of the line, but of course those timestamps are not the only thing in the line so you should remove the anchors.

  • The end condition looks for a timestamp that is not present in your sample, so the match range will never end, printing everything starting at the first match.

Something like this might do approximately what you ask for (outputting 3 with the input file currently shown on the question)

sed -ne '/13:40:47/,/13:41:48/p' somelogfile.log | wc -l
  • Works perfect . Thank you very much for the explanation. – user179309 Jul 12 '16 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.