0

I have a listener.ora file for Oracle in which I need to comment out a block of lines specified by a search string, e.g.,

>(DESCRIPTION=
>>(ADDRESS_LIST=
>>>(ADDRESS=(PROTOCOL=tcp)(HOST=sale-server)(PORT=1521))
>>>(ADDRESS=(PROTOCOL=ipc)(KEY=extproc))))
>SID_LIST_LISTENER=
>>(SID_LIST=
>>>(SID_DESC=
>>>>(GLOBAL_DBNAME=sales)
>>>>(ORACLE_HOME=/oracle9i)
>>>>(SID_NAME=ales)
>>>>(PROGRAM=extproc))
>>>(SID_DESC=
>>>>(SID_NAME=plsextproc)
>>>>(ORACLE_HOME=/oracle9i)
>>>>(PROGRAM=extproc)))

In the above file I have to search for sales (which is db name) and comment out the whole block which contains that string until the next SID_DESC block. e.g., Desired output:

>(DESCRIPTION=  
>>(ADDRESS_LIST=  
>>>(ADDRESS=(PROTOCOL=tcp)(HOST=sale-server)(PORT=1521))  
>>>(ADDRESS=(PROTOCOL=ipc)(KEY=extproc))))  
>SID_LIST_LISTENER=  
>>(SID_LIST=  
>>>#(SID_DESC=  
>>>>#(GLOBAL_DBNAME=sales)  
>>>>#(ORACLE_HOME=/oracle9i)  
>>>>#(SID_NAME=ales)  
>>>>#(PROGRAM=extproc))  
>>>(SID_DESC=  
>>>>(SID_NAME=plsextproc)  
>>>>(ORACLE_HOME=/oracle9i)  
>>>>(PROGRAM=extproc)))

closed as unclear what you're asking by G-Man, Archemar, X Tian, steve, vonbrand Oct 15 '15 at 21:14

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Please add your desired output for that sample input to your question. – Cyrus Oct 15 '15 at 18:02
  • user138725: Seriously, tell us what you want the output to be.  Not a hand-wavy description of it (I don’t know what you mean by “comment”); tell us what you want the output to be.  … (Cont’d) – G-Man Oct 15 '15 at 19:32
  • (Cont’d) …  And please describe what you want to do more explicitly.  I get that you want to do something (unclear) to a “block” that begins with SID_DESC and contains sales.  What defines the end of a block?  A line containing (PROGRAM=extproc) or some portion of that?  A line ending with ))?  Everything up to the next line containing SID_DESC?  What if the block containing sales is the last one in the file, so there is no next line?  And, if the block containing sales is the last one in the file, what do you want done with the ))) at the end of the PROGRAM=extproc line? – G-Man Oct 15 '15 at 19:33
  • Here I want to disable a block of content by searching a database name "sales" inside that block only.There could be many blocks for different databases. For a block of content for any specific database: – user138725 Oct 16 '15 at 6:39
  • Here I want to disable a block of content by searching a database name "sales" inside that block only.There could be many blocks for different databases. For a block of content for any specific database: 1. database name could be anywhere inside that SID_DESC block covered by () 2. other blocks shouldn't be commented(by adding # in begining) 3. A SID_DESC block may have many lines until the next SID_DESC block. so basically, the sed will search for database name and will comment lines before till it finds SID_DESC and after until it finds next SID_DESC line. I hope this explains my problem. – user138725 Oct 16 '15 at 6:45
1

Seems to me like this is not a job for a single sed command but for a complete shell script.

  1. Define a line counter
  2. Define SID markers
  3. Read line by line
  4. Mark each SID location (put them in an array)
  5. Mark each pattern match location (put them in another array)
  6. Check arrays. If the match is between SIDs, comment all lines from first SID to second SID - 1 line
0

It is rather simple task for sed

sed '
    /SID_DESC/{
        /\n/{
            P
            D
            }
        :1
        N
        /PROGRAM/!b1
        /sales/s/[^\n]*\n/#&/g
        }
    ' listener.ora
  • I have flagged the question as unclear and commented on it.  But, given that the OP says, "I need to comment specific block of lines" (emphasis added), and your solution deletes a block of lines, I suspect that this is not what the question is asking for. – G-Man Oct 15 '15 at 19:38
  • @G-Man it is easy to exchange /sales/d for delete by /sales/s/^/#/gm for comment – Costas Oct 15 '15 at 20:02
  • OK, but note that the m option to the s command is a GNU extension (not POSIX-compliant). – G-Man Oct 15 '15 at 20:14
  • @G-Man If you so worry re POSIX-compilant you can use /sales/s/^\|\n/&#/g – Costas Oct 15 '15 at 21:42
  • Here I want to disable a block of content by searching a database name "sales" inside that block only.There could be many blocks for different databases. For a block of content for any specific database: 1. database name could be anywhere inside that SID_DESC block covered by () 2. other blocks shouldn't be commented(by adding # in begining) 3. A SID_DESC block may have many lines until the next SID_DESC block. so basically, the sed will search for database name and will comment lines before till it finds SID_DESC and after until it finds next SID_DESC line. I hope this explains my problem. – user138725 Oct 16 '15 at 6:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.