1

Given I have log file app.log with contents:

2018-08-29 07:43:17.123-0500 CDT [osb_02] [[ACTIVE] ExecuteThread: '14' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO -
 [OSB Tracing] Outbound request was sent.


2018-08-29 07:43:17.456-0500 CDT [osb_02] [[ACTIVE] ExecuteThread: '28' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO -
 [OSB Tracing] Outbound response was received.


2018-08-29 07:44:18.789-0500 CDT [osb_02] [[ACTIVE] ExecuteThread: '14' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO -
 [OSB Tracing] Outbound request was sent.


2018-08-29 07:44:18.779-0500 CDT [osb_02] [[ACTIVE] ExecuteThread: '28' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO -
 [OSB Tracing] Outbound response was received.


2018-08-29 07:45:19.433-0500 CDT [osb_02] [[ACTIVE] ExecuteThread: '14' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO -
 [OSB Tracing] Outbound request was sent.


2018-08-29 07:45:19.998-0500 CDT [osb_02] [[ACTIVE] ExecuteThread: '28' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO -
 [OSB Tracing] Outbound response was received.


2018-08-29 07:46:20.658-0500 CDT [osb_02] [[ACTIVE] ExecuteThread: '14' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO -
 [OSB Tracing] Outbound request was sent.


2018-08-29 07:46:20.774-0500 CDT [osb_02] [[ACTIVE] ExecuteThread: '28' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO -
 [OSB Tracing] Outbound response was received.

I'd like to get log lines between timestamp range I specify as arguments.

Please note, that log line is not just 1 line starting with timestamp. There is additional information after that line until there is next line with different timestamp.

So in our example:

2018-08-29 07:43:17.123-0500 CDT [osb_02] [[ACTIVE] ExecuteThread: '14' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO -
 [OSB Tracing] Outbound request was sent.

is 1 log line.

So, I'd like to execute some command 2018-08-29 07:44:00.000 2018-08-29 07:46:00.000 app.log what will return

2018-08-29 07:44:18.789-0500 CDT [osb_02] [[ACTIVE] ExecuteThread: '14' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO -
 [OSB Tracing] Outbound request was sent.


2018-08-29 07:44:18.779-0500 CDT [osb_02] [[ACTIVE] ExecuteThread: '28' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO -
 [OSB Tracing] Outbound response was received.


2018-08-29 07:45:19.433-0500 CDT [osb_02] [[ACTIVE] ExecuteThread: '14' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO -
 [OSB Tracing] Outbound request was sent.


2018-08-29 07:45:19.998-0500 CDT [osb_02] [[ACTIVE] ExecuteThread: '28' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO -
 [OSB Tracing] Outbound response was received.

Also note, arguments are not necessarily present in log file.

0

sed '1,/pattern1/d;/pattern2/,$d' app.log should do the trick

if you wanted it as a script to run the way you mention, then a function in your ~/.bashrc like the below should do:

time_log() {
    pattern1=$1
    pattern2=$2
    file=$3
    sed "1,/$pattern1/d;/$pattern2/,$d" "$file"
}

then you can run:

time_log "2018-08-29 07:44:" "2018-08-29 07:46:" app.log

edit:

removed uneccessary cat

To include the first line, the easiest way would just to be grep the first line before running the sed:

time_log() {
    pattern1=$1
    pattern2=$2
    file=$3
    grep "$pattern1" "$file"
    sed "1,/$pattern1/d;/$pattern2/,$d" "$file"
}

Though there is probably a better way of doing this with sed if any sed masters would like to chime in.

  • the command doesn't include first timestamp line.. how can it be changed so that it does so? – Patrik Mihalčin Aug 29 '18 at 13:45
  • please see my updated answer :) – RobotJohnny Aug 29 '18 at 15:31
  • it's kind of a hack.. I would like to call only one command.. it might be that awk will be more useful in this scenario – Patrik Mihalčin Sep 3 '18 at 12:21

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.