0

I have a log file containes all information.

2017-09-21 02:01:11,130 [http-nio-8080-exec-5] INFO   - Inbound Message
...
...
2017-09-21 09:01:11,130 [http-nio-8080-exec-5] INFO   - Inbound Message
----------------------------
ID: 6044
Address: http://localhost/serveur/Service?wsdl
Encoding: UTF-8
Http-Method: POST
...
2017-09-21 12:01:11,130 [http-nio-8080-exec-5] INFO   - Inbound Message
...

I want to extract only information between 2 dates. For exemple, if I want information between 2017-09-21 09.. and 2017-09-21 12 I would have something like this:

./script.sh 2017-09-21 09 2017-09-21 12
# $1 = 2017-09-21
# $2 = 09
# $3 = 2017-09-21
# $4 = 12
# $1 & 2 are date and hour to begin extract
# $3 & 4 are date and hour to finish extract

my script is like this. I don't have knowledge in shell programming but I tried to do this. Unfortunatly it doesn't work

#!/bin/bash

beginLine = "$1 $2"
finishLine = "$3 $4"

while read fileLog.log
do
    if($fileLog.log grep beginLine)
         echo $fileLog.log >> extractedlog.log
    fi
    if($fileLog.log grep finishLine)
         < extractedLog.log;
    fi
done
exit 0;

Can anyone tell me where is the problem?

Chinovski
  • 497
  • 3
  • 7
  • 18

2 Answers2

2
$ awk '/2017-09-21 09/{a=1};a;/2017-09-21 12/{exit}' input
2017-09-21 09:01:11,130 [http-nio-8080-exec-5] INFO   - Inbound Message
----------------------------
ID: 6044
Address: http://localhost/serveur/Service?wsdl
Encoding: UTF-8
Http-Method: POST
...
2017-09-21 12:01:11,130 [http-nio-8080-exec-5] INFO   - Inbound Message

More alternatives found here

mathB
  • 634
  • 8
  • 21
2

Short sed approach (matching a range of patterns /.../,/.../):

sed -n '/2017-09-21 09/,/2017-09-21 12/p' logfile

----------

The same can be done with awk (even shorter):

awk '/2017-09-21 09/,/2017-09-21 12/' logfile
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105