Tnsnames.ora

Hi,

I would like to modify, in script schell, the line right above (DESCRIPTION and check three cases :

if line contain ".world" then line=line-".world" concat "," concat line
if line dont contain ".world" then line=line concat "," concat line concat".world"
else line=line

Keep in mind that there may or may not be spaces between the '(' and the word DESCRIPTION. So valid examples can be something like this:

PRI =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.3.7)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = pri)
      (INSTANCE_NAME = pri)
    )
  )

STDBY.world =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.3.5)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = stdby)
      (INSTANCE_NAME = stdby)
    )
  )


AIXSNAP.us.net =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.9.0.5)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = aixsnap)
      (INSTANCE_NAME = aixsnap)
    )
  )

only the lines PRI and STDBY.world can be modified as :

PRI,PRI.world=
and 
STDBY,STDBY.world=

Many thanks

If we check for contains ".world" and doesn't contain ".world", where do we use else case?

I mean we check if is string or string.world.
If it is many_strings , like aixsnap.us.net, then we consider it in others and don't modify.

Many thanks.

From python shell:

>>> text = '''
... PRI =
...   (DESCRIPTION =
...     (ADDRESS_LIST =
...       (ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.3.7)(PORT = 1521))
...     )
...     (CONNECT_DATA =
...       (SERVER = DEDICATED)
...       (SERVICE_NAME = pri)
...       (INSTANCE_NAME = pri)
...     )
...   )
... 
... STDBY.world =
...   (DESCRIPTION =
...     (ADDRESS_LIST =
...       (ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.3.5)(PORT = 1521))
...     )
...     (CONNECT_DATA =
...       (SERVER = DEDICATED)
...       (SERVICE_NAME = stdby)
...       (INSTANCE_NAME = stdby)
...     )
...   )
... 
... 
... AIXSNAP.us.net =
...   (DESCRIPTION =
...     (ADDRESS_LIST =
...       (ADDRESS = (PROTOCOL = TCP)(HOST = 10.9.0.5)(PORT = 1521))
...     )
...     (CONNECT_DATA =
...       (SERVER = DEDICATED)
...       (SERVICE_NAME = aixsnap)
...       (INSTANCE_NAME = aixsnap)
...     )
...   )
... '''
>>> 
>>> print re.sub(re.compile(r'^\s*(\w+)(?:\.world|\s*)(?=\s*=\s*\n\s*[\(]\s*DESCRIPTION)',re.M),r'\1,\1.world',text)
PRI,PRI.world=
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.3.7)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = pri)
      (INSTANCE_NAME = pri)
    )
  )
STDBY,STDBY.world =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.3.5)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = stdby)
      (INSTANCE_NAME = stdby)
    )
  )


AIXSNAP.us.net =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.9.0.5)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = aixsnap)
      (INSTANCE_NAME = aixsnap)
    )
  )

In a script:

# cat lsnr_sub.py 
#!/usr/bin/env python
import sys
import re

try:
   with open(sys.argv[1],'r') as f:
      text = f.read()
except:
   print "err with file"
   sys.exit(5)

reg_ex = (r'^\s*(\w+)(?:\.world|\s*)'
          r'(?=\s*=\s*\n\s*[\(]\s*DESCRIPTION)')
print re.sub(re.compile(reg_ex,re.M),r'\1,\1.world',text)

Usage:

# ./lsnr_sub.py listener.ora

Thank you klashxx, is it possible to have it in ksh please ?

---------- Post updated at 11:45 AM ---------- Previous update was at 11:03 AM ----------

I receive a syntax error for line 6 : open...
Could you reply in ksh please ?

---------- Post updated 28-03-14 at 12:09 AM ---------- Previous update was 27-03-14 at 11:45 AM ----------

Hi,
i am not familiar with python, so when i run the command line :

>>> print re.sub(re.compile(r'^\s*(\w+)(?:\.world|\s*)(?=\s*=\s*\n\s*[\(]\s*DESCRIPTION)',re.M),r'\1,\1.world',text)
with adding ( import re) 

it run very well, however when i run the script i receive the syntax error in line 6.
Thank you for reply.

Here is an awk solution:

awk '
/\(DESCRIPTION/{
   if(index(prev,".world")) sub(/^[^.]+/,"&,&",prev)
   if(!index(prev,".")) sub(/^[^ ]+/,"&,&.world",prev)
}
NR>1 {print prev}
{prev=$0}
END {print prev}' infile

Many thanks Chubler_XL,
so, how can i use it in a script please ?

Just past it directly into your script if you want to overwrite the original file try something like this:

awk '
/\(DESCRIPTION/{
...
END {print prev}' infile > /tmp/keepp_$$
mv -f keepp_$$ infile

Hi Chubler_XL, i made it in script and it works very nice, but in case when string without ".world" i receive

string=,string=.world

Could you reply please

In your examples you have a space between string and =
I'm guessing this space is optional.

Try:

awk '
/\(DESCRIPTION/{
   if(index(prev,".world")) sub(/^[^.]+/,"&,&",prev)
   if(!index(prev,".")) sub(/^[^ =]+/,"&,&.world",prev)
}
NR>1 {print prev}
{prev=$0}
END {print prev}' infile