Python search string with similar name in oratab file

Viewed 223

I have written script in python in which this script will take name from user and search in a file after getting result it will be splited and will export to OS environment variable.

The problem is that while searching name in a file their are two name say GOCO and GOCO01 and if user enter GOCO but result output or export output is GOCO01 because it is coming first in file ocurrance i want if user select GOCO then GOCO is to be exported

My script:-

name_db = input("Please enter a Database Name: ")
with open("/etc/oratab") as f:
    for line in f:
     if name_db in line:
      var1= line
   os.environ['ORACLE_SID']=var1.split(':')[0]
   os.environ['ORACLE_HOME']=var1.split(':')[1]

print(name_db)

GOCO

print (os.environ['ORACLE_SID'])

GOCO01

ORATAB File Content:-

ATING001:/home/oracle/dbhome_1:Y
GOCO01:/home/oracle/dbhome_1:Y
GOCO:/home/oracle/dbhome_1:Y
SYM1:/home/oracle/dbhome_1:Y
SYM2:/home/oracle/dbhome_1:Y

I want output to be

print (os.environ['ORACLE_SID'])

GOCO

2 Answers
Related