rcubefather
rcubefather

Reputation: 1564

How to match a number in middle using python regular expression?

I am trying to put PASS/FAIL criteria for the following command output.

router-7F2C13#show app stats gmail on TEST/switch1234-15E8CC
--------------------------------------------------------------------------------
     APPLICATION           BYTES_IN         BYTES_OUT           NUM_FLOWS
--------------------------------------------------------------------------------
  gmail                0                 0                  0
--------------------------------------------------------------------------------
router-7F2C13#

I need to match only "NUM_FLOWS". If its "zero" then it will be considering as FAIL. If its "Greater than or equal to 1" then it will be considering as PASS.

FAIL criteria example:
======================

router-7F2C13#show app stats gmail on TEST/switch1234-15E8CC
--------------------------------------------------------------------------------
     APPLICATION           BYTES_IN         BYTES_OUT           NUM_FLOWS
--------------------------------------------------------------------------------
  gmail                0                 0                  0
--------------------------------------------------------------------------------
router-7F2C13#



PASS criteria example: (Greater than or equal to 1)
======================

router-7F2C13#show app stats gmail on TEST/switch1234-15E8CC
--------------------------------------------------------------------------------
     APPLICATION           BYTES_IN         BYTES_OUT           NUM_FLOWS
--------------------------------------------------------------------------------
  gmail                0                 0                  1
--------------------------------------------------------------------------------
router-7F2C13#

Please guide me on how to do this.

Upvotes: 0

Views: 59

Answers (2)

Mark Tolonen
Mark Tolonen

Reputation: 177715

Since this is command output you can pipe the output of the command into a Python script and process it with the fileinput module:

import fileinput
for line in fileinput.input():
    if fileinput.filelineno() == 5:
        # split the line on whitespace, and take the last item.
        x = int(line.split()[-1])
        print('PASS' if x else 'FAIL')
        break # so it won't process more lines past 5

Result with pass.xxx containing your pass criteria and fail.xxx containing your fail criteria:

c:\> type fail.xxx | check.py
FAIL

c:\> type pass.xxx | check.py
PASS

Upvotes: 1

vks
vks

Reputation: 67968

NUM_FLOWS\n-+[\s\S]*?(\d+)\s*-+

You can try this.Grab the capture.

See demo.

https://www.regex101.com/r/bC8aZ4/7

x="""router-7F2C13#show app stats gmail on TEST/switch1234-15E8CC
--------------------------------------------------------------------------------
     APPLICATION           BYTES_IN         BYTES_OUT           NUM_FLOWS
--------------------------------------------------------------------------------
  gmail                0                 0                  0
--------------------------------------------------------------------------------
router-7F2C13#
---------------------------------------------------------------
router-7F2C13#"""
if int(re.findall(r"NUM_FLOWS\n-+[\s\S]*?(\d+)\s*-+",x)[0]):
      print "pass"
else:
      print "fail"

Upvotes: 1

Related Questions