-3

i have a log text like this.

data = '''
================================================================================
Annotation file:
/b/cd-builder/sandboxes/sb_DEV_COMMON_BRANCH-_act-builder-4977/annofile-4977TB53661_cd-builder.xml 

Build ID: 27951 on Host bng-emake-5a.juniper.net, Cluster Manager: bng-ea-cm-02:8030
Start Time: Sun Jun 17 23:57:30 2018


Job ID: J00002adde8677420 , Exit Value 1
CWD: /b/cd-builder/sandboxes/sb_DEV_COMMON_BRANCH-_act-builder-4977/obj/bsd11/amd64/junos/usr.sbin/rpd/bgp/lib/proto
Node: bng-ea-agent-14a-3  Start: 2018-06-18 00:20:55.488609 (1405.488609)
                          End:   2018-06-18 00:20:56.653945 (1406.653945)

Command:

export BMAKELOCATION=/b/cd-builder/sandboxes/sb_DEV_COMMON_BRANCH-_act-builder-4977/src/build/mk/jnx.sym_check.mk:37; CURDIR=/b/cd-builder/sandboxes/sb_DEV_COMMON_BRANCH-_act-builder-4977/src/junos/usr.sbin/rpd/bgp/lib/proto  OBJDUMP=/volume/hab/Linux/Ubuntu-12.04/x86_64/llvm/3.7/current/bin/amd64-unknown-freebsd11.0-objdump  BSS_SYMS=bss_syms.clang.amd64,bsdx  TLS_SYMS=tls_syms.clang.amd64,bsdx  ACCEPT_CMD='mk --machine amd64,bsd11 -C junos/usr.sbin/rpd/bgp/lib/proto accept-syms'  /volume/hab/Linux/Ubuntu-12.04/x86_64/bsd-tools/current/bin/sh /b/cd-builder/sandboxes/sb_DEV_COMMON_BRANCH-_act-builder-4977/src/build/scripts/sym_check.sh librpd-proto-bgp.a

------------------------------ Output ------------------------------
ERROR: /b/cd-builder/sandboxes/sb_DEV_COMMON_BRANCH-_act-builder-4977/src/junos/usr.sbin/rpd/bgp/lib/proto/tls_syms.clang.amd64,bsdx: changed global or static TLS variables
261a262
> bgp_inetsrte_incolor_update._xinfo

If correct, use:

    mk --machine amd64,bsd11 -C junos/usr.sbin/rpd/bgp/lib/proto accept-syms

make[1]: *** [check-syms] Error 1
--------------------------------------------------------------------

Operations:

================================================================================
+ echo -e '\e[31m Displaying Production.log.errs\e[0m'
 Displaying Production.log.errs
+ echo 'End Meta Error logs'
End Meta Error logs
'''

from this log text, I need to extract the text between "------------------------------ Output ------------------------------" and "--------------------------------------------------------------------" so the output will be like this.

output = '''
ERROR: /b/cd-builder/sandboxes/sb_DEV_COMMON_BRANCH-_act-builder-4977/src/junos/usr.sbin/rpd/bgp/lib/proto/tls_syms.clang.amd64,bsdx: changed global or static TLS variables
261a262
> bgp_inetsrte_incolor_update._xinfo

If correct, use:

    mk --machine amd64,bsd11 -C junos/usr.sbin/rpd/bgp/lib/proto accept-syms

make[1]: *** [check-syms] Error 1
'''

not sure why my below code is not working.

for result in re.findall(r'------------------------------ Output ------------------------------(.*?) '
                         '--------------------------------------------------------------------', data, re.S):
    output += result
om tripathi
  • 300
  • 1
  • 5
  • 20

5 Answers5

2

This might help. Regex Lookbehind & Lookahead

Demo:

import re
for result in re.findall(r'(?<=------------------------------ Output ------------------------------)(.*?)(?=--------------------------------------------------------------------)', data, re.S):    
    print(result)
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • 1
    @omtripathi This regex is the same as your solution (see no space after `(.*?)` as I suggested), just use this same code with your pattern minus the space. You may as well remove the `?<=` and `?=` and use `r'------------------------------ Output ------------------------------(.*?)--------------------------------------------------------------------'` – Wiktor Stribiżew Jun 18 '18 at 07:52
1

You don't have to match regex to do that. you can use find instead:

start = "------------------------------ Output ------------------------------"
end = "--------------------------------------------------------------------"

extracted_text = data[data.find(start)+len(start):data.rfind(end)] 

the output will be:

ERROR: /b/cd-builder/sandboxes/sb_DEV_COMMON_BRANCH-_act-builder-4977/src/junos/usr.sbin/rpd/bgp/lib/proto/tls_syms.clang.amd64,bsdx: changed global or static TLS variables 261a262

bgp_inetsrte_incolor_update._xinfo

If correct, use:

mk --machine amd64,bsd11 -C junos/usr.sbin/rpd/bgp/lib/proto accept-syms

make[1]: *** [check-syms] Error 1

AndreyF
  • 1,798
  • 1
  • 14
  • 25
1

regex is overkilling it. Your start / stop is unique thus the following would work:

start = "------------------------------ Output ------------------------------"
stop = "--------------------------------------------------------------------"
output = log.split(start)[1].split(stop)[0]
Mathieu
  • 5,410
  • 6
  • 28
  • 55
1
output = ""
for result in re.findall(r'------------------------------ Output ------------------------------(.*?)'
                     '--------------------------------------------------------------------', data, re.S):
output += result

print output

The regex was not matching anything because of the space after (.*?).

Negi Babu
  • 507
  • 4
  • 11
-1

And what about it?

reg=re.compile(r"-{10,} Output -{10,}(.*?)-{10,}",re.S)
rslt=reg.findall(data)
kantal
  • 2,331
  • 2
  • 8
  • 15