Posts: 19
Threads: 4
Joined: Oct 2018
Oct-18-2018, 10:44 AM
(This post was last modified: Oct-18-2018, 10:53 AM by buran.)
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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
Posts: 8,167
Threads: 160
Joined: Sep 2016
Oct-18-2018, 10:53 AM
(This post was last modified: Oct-18-2018, 10:53 AM by buran.)
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
Posts: 19
Threads: 4
Joined: Oct 2018
Oct-18-2018, 10:55 AM
(This post was last modified: Oct-18-2018, 10:58 AM by alinaveed786.)
Yes, it's the syntax error. Any other way to pass list values as patterns to re.search or re.findall ?
Posts: 8,167
Threads: 160
Joined: Sep 2016
you need to use RegEx. If you are not familiar with regex expression syntax read https://docs.python.org/3/library/re.htm...ion-syntax and also https://www.regular-expressions.info/
you can use http://regex101.com to test your regex expression
Posts: 19
Threads: 4
Joined: Oct 2018
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 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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
1 |
Patch [ 27923320 , 27547329 , 21463894 ] detected
|
Posts: 4,220
Threads: 97
Joined: Sep 2016
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???
Posts: 8,167
Threads: 160
Joined: Sep 2016
(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()
Posts: 19
Threads: 4
Joined: Oct 2018
Oct-20-2018, 05:57 AM
(This post was last modified: Oct-20-2018, 05:57 AM by alinaveed786.)
I was able to achieve via below modifications. Thanks everyone for all the help and support
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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" )
|
Posts: 566
Threads: 10
Joined: Apr 2017
(Oct-20-2018, 05:57 AM)alinaveed786 Wrote: I was able to achieve via below modifications. Thanks everyone for all the help and support
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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?!
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
|