Dec-17-2024, 10:48 AM
(This post was last modified: Dec-17-2024, 12:34 PM by Gribouillis.)
Hello all,
I have the following file with DB_BASE commented:
profile.txt
export DB_SID=NEW
#export DB_BASE=/oracle/${DB_SID}
export DB_HOME=$DB_BASE/19
export PATH=$PATH:$DB_HOME/bin
export NLS_LANG=AMERICAN_AMERICA.UTF8
I need to search in the file and using regular expressions to check if DB_BASE is commented or not. Below is my match:
Thank you,
I have the following file with DB_BASE commented:
profile.txt
export DB_SID=NEW
#export DB_BASE=/oracle/${DB_SID}
export DB_HOME=$DB_BASE/19
export PATH=$PATH:$DB_HOME/bin
export NLS_LANG=AMERICAN_AMERICA.UTF8
I need to search in the file and using regular expressions to check if DB_BASE is commented or not. Below is my match:
match = re.search('r(.*)#.*DB_BASE|.*DB_BASE',line)and the code:
import re with open('profile.txt', 'r') as profile_file: file_content = profile_file.readlines() #print(file_content) file_content_new = list(map(lambda x:x.strip(),file_content)) #print(file_content_new) for line in file_content_new: match = re.search('r(.*)#.*DB_BASE|.*DB_BASE',line) print(match) if match: if match.group(1) is not None and re.search(r'#', match.group(1)): print('DB_BASE is commented') else: print('DB_BASE is not commented')It doesn't matter what I am doing, i receive always that the DB is not commented....
Output:['export DB_SID=NEW\n', '#export DB_BASE=/oracle/${DB_SID}\n', 'export DB_HOME=$DB_BASE/19\n', 'export PATH=$PATH:$DB_HOME/bin\n', 'export NLS_LANG=AMERICAN_AMERICA.UTF8\n']
['export DB_SID=NEW', '#export DB_BASE=/oracle/${DB_SID}', 'export DB_HOME=$DB_BASE/19', 'export PATH=$PATH:$DB_HOME/bin', 'export NLS_LANG=AMERICAN_AMERICA.UTF8']
None
DB_BASE is not commented
<_sre.SRE_Match object at 0x7f5479ccf230>
<_sre.SRE_Match object at 0x7f5479cd95b0>
None
DB_BASE is not commented
None
DB_BASE is not commented
The logic is not correct, or am I doing something wrong....Could you please help me?Thank you,
Gribouillis write Dec-17-2024, 12:34 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.