Converta multilinhas em uma única linha com o SED

1

Preciso de ajuda adicional, com base em uma pergunta semelhante que localizei aqui: Converta multilinhas em uma única linha

Exemplo atual de SED

(sed  -e'y/)},={/(((((/' \
  -e's/-\([^(I]*\)[^0-9]*\([0-9]*\)[( ]*/-  -/;=' |
paste -d- - - |
sort  -t- -nk3,3 -nk1,1 |
sed   -e's/^[^-]*-//;:n' -e'h;$!N' \
  -e's/\(-\([^-]*-\).*[^ ]\) *\n\([^-]*-\)\{2\}/ - /;tn' \
  -ex\;:t -e's/\(\([^-]*-\)[^/]*\) - */,/;tt'   -e'p;g;D' ) \
< in.txt > out.txt

Seguindo o registro de múltiplas linhas

6/13/2015 12:00:47 AM - {   562} START Web 
6/13/2015 12:00:47 AM - Requested Web connection from 123.125.71.103[123.125.71.103], ID=562 
6/13/2015 12:01:24 AM - {   563} START POP3 
6/13/2015 12:01:24 AM - Requested POP3 connection from 10.127.251.37 [10.127.251.37], ID=563 
6/13/2015 12:01:24 AM - (   563) USER [email protected] 
6/13/2015 12:01:24 AM - POP3 connection with 10.127.251.37 [10.127.251.37] ended. ID=563 
6/13/2015 12:01:24 AM - {   563} END POP3
6/13/2015 12:01:24 AM - {   564} START POP3 
6/13/2015 12:01:24 AM - Requested POP3 connection from 10.127.251.37 [10.127.251.37], ID=564 
6/13/2015 12:01:24 AM - (   564) USER [email protected] 
6/13/2015 12:01:24 AM - POP3 connection with 10.127.251.37 [10.127.251.37] ended. ID=564 
6/13/2015 12:01:24 AM - {   564} END POP3
6/13/2015 12:01:40 AM - Web connection with 123.125.71.103 [123.125.71.103] ended. ID=562 
6/13/2015 12:01:40 AM - {   562} END Web

é convertido para

6/13/2015 12:00:47 AM - 562 - START Web, Requested Web connection from 123.125.71.103 [123.125.71.103] - 6/13/2015 12:01:40 AM - Web connection with 123.125.71.103 [123.125.71.103] ended., END Web
6/13/2015 12:01:24 AM - 563 - START POP3, Requested POP3 connection from 10.127.251.37 [10.127.251.37], USER [email protected], POP3 connection with 10.127.251.37 [10.127.251.37] ended., END POP3
6/13/2015 12:01:24 AM - 564 - START POP3, Requested POP3 connection from 10.127.251.37 [10.127.251.37], USER [email protected], POP3 connection with 10.127.251.37 [10.127.251.37] ended., END POP3

Eu gostaria de modificar o SED para incluir também os seguintes registros onde os IDs estão entre colchetes (exemplo: [ 667] ).

Exemplo de log

6/13/2015 2:15:09 AM - Starting delivery thread, ID=667
6/13/2015 2:15:09 AM - {   667} START Delivery
6/13/2015 2:15:09 AM - Active delivery threads (ID=667 start): 0
6/13/2015 2:15:09 AM - [   667] Delivering to [email protected]
6/13/2015 2:15:09 AM - [   667] Forwarding mail...
6/13/2015 2:15:09 AM - [   667] Forwarding message to 1 address(es)
6/13/2015 2:15:09 AM - Delivery thread, ID=667, terminated after running for 0.031 seconds.
6/13/2015 2:15:09 AM - {   667} END Delivery
6/13/2015 2:15:09 AM - Active delivery threads (ID=667 end): 0

Segunda pergunta que eu tenho é como eu deveria ignorar logs onde não há ID. Isso não é tão crítico, mas seria bom filtrar.

Por exemplo:

6/13/2015 2:43:24 AM - Exended logging
6/13/2015 2:43:24 AM - Setting msgSizeLimit 15

Obrigado antecipadamente.

    
por mmesojedec 28.07.2015 / 10:23

1 resposta

1

É apenas o primeiro sed que precisa ser modificado para lidar com todas as possibilidades listadas aqui. Especificamente, em vez de lidar com todas as possibilidades em um único regexp, você precisará abordar dois tipos diferentes de linhas separadamente - o tipo no qual o ID segue imediatamente o primeiro - traço no uma linha e o outro tipo em que ela é encontrada rotulada como ID= em outro lugar.

(   sed -e= -e's/- [({[] *\([0-9]*\) *[])}]/-  -/;t' \
            -e's/-\(.*[^, ]\)[, ]*ID=\([0-9]*\)[, ]\{0,1\}/-  -/' |
    paste   -d- - - |
    sort    -t- -nk3,3 -nk1,1 |
    sed     -e's/^[^-]*-//;:n' -e'h;$!N' \
            -e's/\(-\([^-]*-\).*[^ ]\) *\n\([^-]*-\)\{2\}/ - /;tn' \
            -ex\;:t -e's/\(\([^-]*-\)[^/]*\)- */:::/;tt'   -e'p;g;D'
) <in >out

Aqui está uma segunda vez, mas escrita com a sintaxe de expressão regular -E xtended. Isso funcionará pelo menos com BSD / GNU / AST sed s:

(   sed -Ee= -e's/- [({[] *([0-9]+) *[])}]/-  -/;t' \
             -e's/-(.*[^, ])[, ]*ID=([0-9]+)[, ]?/-  -/' |
    paste    -d- - - |
    sort     -t- -nk3,3 -nk1,1 |
    sed      -Ee's/^[^-]*-//;:n' -e'h;$!N' \
             -e's/(-([^-]+-).*[^ ]) *\n([^-]+-){2}/ - /;tn' \
             -ex\;:t -e's/(([^-]+-)[^/]*)- */:::/;tt' -e'p;g;D'
) <in >out

Agora testei isso em uma concatenação de todos os seus dados de registro:

6/13/2015 12:00:47 AM - {   562} START Web
6/13/2015 12:00:47 AM - Requested Web connection from 123.125.71.103[123.125.71.103], ID=562
6/13/2015 12:01:24 AM - {   563} START POP3
6/13/2015 12:01:24 AM - Requested POP3 connection from 10.127.251.37 [10.127.251.37], ID=563
6/13/2015 12:01:24 AM - (   563) USER [email protected]
6/13/2015 12:01:24 AM - POP3 connection with 10.127.251.37 [10.127.251.37] ended. ID=563
6/13/2015 12:01:24 AM - {   563} END POP3
6/13/2015 12:01:24 AM - {   564} START POP3
6/13/2015 12:01:24 AM - Requested POP3 connection from 10.127.251.37 [10.127.251.37], ID=564
6/13/2015 12:01:24 AM - (   564) USER [email protected]
6/13/2015 12:01:24 AM - POP3 connection with 10.127.251.37 [10.127.251.37] ended. ID=564
6/13/2015 12:01:24 AM - {   564} END POP3
6/13/2015 12:01:40 AM - Web connection with 123.125.71.103 [123.125.71.103] ended. ID=562
6/13/2015 12:01:40 AM - {   562} END Web
6/13/2015 2:15:09 AM - Starting delivery thread, ID=667
6/13/2015 2:15:09 AM - {   667} START Delivery
6/13/2015 2:15:09 AM - Active delivery threads (ID=667 start): 0
6/13/2015 2:15:09 AM - [   667] Delivering to [email protected]
6/13/2015 2:15:09 AM - [   667] Forwarding mail...
6/13/2015 2:15:09 AM - [   667] Forwarding message to 1 address(es)
6/13/2015 2:15:09 AM - Delivery thread, ID=667, terminated after running for 0.031 seconds.
6/13/2015 2:15:09 AM - {   667} END Delivery
6/13/2015 2:15:09 AM - Active delivery threads (ID=667 end): 0

E meus resultados foram:

6/13/2015 12:00:47 AM - 562 - START Web ::: Requested Web connection from 123.125.71.103[123.125.71.103] - 6/13/2015 12:01:40 AM - Web connection with 123.125.71.103 [123.125.71.103] ended. ::: END Web
6/13/2015 12:01:24 AM - 563 - START POP3 ::: Requested POP3 connection from 10.127.251.37 [10.127.251.37] ::: USER [email protected] ::: POP3 connection with 10.127.251.37 [10.127.251.37] ended. ::: END POP3
6/13/2015 12:01:24 AM - 564 - START POP3 ::: Requested POP3 connection from 10.127.251.37 [10.127.251.37] ::: USER [email protected] ::: POP3 connection with 10.127.251.37 [10.127.251.37] ended. ::: END POP3
6/13/2015 2:15:09 AM - 667 - Starting delivery thread ::: START Delivery ::: Active delivery threads (start): 0 ::: Delivering to [email protected] ::: Forwarding mail... ::: Forwarding message to 1 address(es) ::: Delivery thread terminated after running for 0.031 seconds. ::: END Delivery ::: Active delivery threads (end): 0
    
por 28.07.2015 / 12:07