I m stuck with re.findall . Basically, below code, I m trying to find whether certain patches as defined by patches list is applied to the Oracle home or not.
#!/usr/bin/python
import re
import subprocess
s = subprocess.check_output(["opatch", "lsinventory"])
output = s.decode("utf-8")
patches = [ 27923320,27547329,21463894]
searchObj = re.findall(r patches[1:], output, re.M|re.I)
if searchObj:
print ( searchObj.group(),"detected")
else:
print ("Nothing found!!")
below the error
Error:
/u02/scripts/Patching/venv/bin/python /u02/scripts/Patching/Patch.py
File "/u02/scripts/Patching/Patch.py", line 16
searchObj = re.findall(r patches[1:], output, re.M|re.I)
^
SyntaxError: invalid syntax
Process finished with exit code 1
Can you show us what you get as an output from subprocess.check_output()? Probably [with some transformation of the output] you can use set operations and not RegEx.
The error is because r patches[1:]
is not valid code - that's clear
Yes, it's the syntax error. Any other way to pass list values as patterns to re.search or re.findall ?
I was able to figure out the correct usage of re.findall but I want each list value in the new line.
Can we use for loop to pass list value to re.findall and then print each output in a new line ?
#!/usr/bin/python
import os
import re
import subprocess
s = subprocess.check_output(["opatch", "lsinventory"])
output = s.decode("utf-8")
patches = [27923320, 27547329, 21463894]
searchObj = re.findall(str(patches), output, re.M|re.I)
if searchObj:
print('Patch', patches, "detected")
else:
print("Nothing found!!")
The output
Patch [27923320, 27547329, 21463894] detected
You have a list of numbers. Regexes are for strings, not lists and not numbers. Do you want to test the regex on the string version of the numbers???
(Oct-18-2018, 01:07 PM)ichabod801 Wrote: [ -> ]Do you want to test the regex on the string version of the numbers???
I think OP want to create RegEx pattern(s) from this list of numbers and test the output they get from
subprocess.check_output()
I was able to achieve via below modifications. Thanks everyone for all the help and support
#!/usr/bin/python
import re
import subprocess
s = subprocess.check_output(["opatch", "lsinventory"])
output = s.decode("utf-8")
patches = [27923320, 27547329, 21463894, 12345, 99999]
patches_found = set(re.findall(r'\b(?:%s)\b' % '|'.join(map(str, patches)), output))
patches_missing = set(map(str, patches)) - patches_found
if patches_found:
print('Patch', patches_found, "detected")
if patches_missing:
print("Patch", patches_missing, "missing")
(Oct-20-2018, 05:57 AM)alinaveed786 Wrote: [ -> ]I was able to achieve via below modifications. Thanks everyone for all the help and support
#!/usr/bin/python
import re
import subprocess
s = subprocess.check_output(["opatch", "lsinventory"])
output = s.decode("utf-8")
patches = [27923320, 27547329, 21463894, 12345, 99999]
patches_found = set(re.findall(r'\b(?:%s)\b' % '|'.join(map(str, patches)), output))
patches_missing = set(map(str, patches)) - patches_found
if patches_found:
print('Patch', patches_found, "detected")
if patches_missing:
print("Patch", patches_missing, "missing")
You convert
patches
elements to strings twice - would not it have been easier to just define them as strings?!