Removing section from tnsnames.ora

Hi,
I am trying to write a script or command to remove a section from tnsnames.ora file

in the following example I would like to remove tns_alias2 section

$ cat tnsnames.ora
tns_alias1 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = host1 )(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = service1)
    )
  )

tns_alias2 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = host2 )(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = service2)
    )
  )

tns_alias3 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = host3 )(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = service3)
    )
  )

$ sed ......

$ cat tnsnames.ora
tns_alias1 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = host1 )(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = service1)
    )
  )

tns_alias3 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = host3 )(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = service3)
    )
  )

any ideas ?

Try:

sed '/tns_alias2 /,/^ *$/d' file

or

awk '$1!="tns_alias2"' RS= ORS='\n\n' file

The commands use empty lines as section separator, so a precondition is that they are present between blocks. That awk example only works if the separating lines are completely empty (i.e. two consecutive newlines)..

Thanks, the sed command works fine on that file, but the file can not always has spaces like I mentioned.
The file can be like this:

$ cat tnsnames.ora
tns_alias1=
(DESCRIPTION=
(ADDRESS_LIST=
(ADDRESS=(PROTOCOL=TCP)(HOST=host1)(PORT=1521))
)
(CONNECT_DATA=
(SERVICE_NAME=service1)
)
)
tns_alias2=
(DESCRIPTION=
(ADDRESS_LIST=
(ADDRESS=(PROTOCOL=TCP)(HOST=host2)(PORT=1521))
)
(CONNECT_DATA=
(SERVICE_NAME=service2)
)
)
tns_alias3=
(DESCRIPTION=
(ADDRESS_LIST=
(ADDRESS=(PROTOCOL=TCP)(HOST=host3)(PORT=1521))
)
(CONNECT_DATA=
(SERVICE_NAME=service3)
)
)

Might benefit from some fine tuning:

awk '
/^tns_alias2/   {getline
                 L+=gsub (/\(/, "") - gsub (/\)/,"")
                }

!L
L               {L+=gsub (/\(/, "") - gsub (/\)/,"")
                }

' file
1 Like