Linhas de log de saída entre startTime e endTime

1

Dado que eu tenho o arquivo de log app.log com o conteúdo:

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.

Gostaria de obter linhas de registro entre o intervalo de registro de data e hora que eu especifico como argumentos.

Por favor, note que a linha de log não é apenas uma linha começando com o timestamp. Há informações adicionais depois dessa linha até que haja a próxima linha com um registro de data e hora diferente.

Então, no nosso exemplo:

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.

é 1 linha de log.

Então, eu gostaria de executar some command 2018-08-29 07:44:00.000 2018-08-29 07:46:00.000 app.log o que retornará

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.

Observe também que os argumentos não estão necessariamente presentes no arquivo de log.

    
por Patrik Mihalčin 29.08.2018 / 15:21

1 resposta

0

sed '1,/pattern1/d;/pattern2/,$d' app.log deve fazer o truque

se você quisesse que um script fosse executado da maneira que você menciona, então uma função em seu ~ / .bashrc como o abaixo deveria ser:

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

então você pode executar:

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

editar:

removeu cat desnecessária

Para incluir a primeira linha, a maneira mais fácil seria apenas grep da primeira linha antes de executar o sed :

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

Embora haja provavelmente uma maneira melhor de fazer isso com sed se algum mestre de sed quiser entrar em contato.

    
por 29.08.2018 / 15:37

Tags