Dec-17-2024, 10:48 AM
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,