Search code examples
bashshellawkinsertion

How to insert a commentary line in a PL/SQL file


Here's my problem, I want to had in PL/SQL code commentary with SVN's Keywords. The problem is that in production the commentaries above the CREATE OR REPLACE is ereased when compiling.

Then to do this I have to add the commentary lines just after the line CREATE OR REPLACE [name] [AS/IS]. My problem is that I have absolutely no idea to process with a Shell script. I think I would use awk but I'm not a master of this tool at all :S

For exemple I got a file like this:

-- Comments here will be deleted when compiling
-- That's why i must had my comment after the CREATE OR REPLACE
CREATE OR REPLACE PACKAGE BODY example_file
IS
   -------------------------------------------------------------
   --  Version     Date          Person    Comments
   -------------------------------------------------------------
   CODE
   .
   .
   .

And I want a file like that:

-- Comments here will be deleted when compiling
-- That's why i must had my comment after the CREATE OR REPLACE
CREATE OR REPLACE PACKAGE BODY example_file
IS
   -- REVISION $Revision$
   -- ID $Id$
   -------------------------------------------------------------
   --  Version     Date          Person    Comments
   -------------------------------------------------------------
   CODE
   .
   .
   .

Of course I could write it one by one in each file. But there's a lot of files that's why I'm looking for a way to do this really faster.

Thanks,

Ežekiel.


Solution

  • awk to the rescue!

    $ awk '/CREATE OR REPLACE/{n=NR} 
               /^IS/ && NR==n+1{print; 
                                print "   -- REVISION ...";
                                print "   -- ID ...";
                                next
                               } 1' sql
    

    mark the first pattern "CREATE..." and if the next line starts with "IS" insert the two comment lines.

    UPDATE: now looks for AS or IS, not just at the next line but between the same line and 3 following lines

    $ awk        '/CREATE OR REPLACE/{n=NR} 
      /\yIS|AS\y/ && n<=NR && NR<=n+3{
                 print; 
                 print "  -- REVISION ...\n  -- ID ...";
                 next} 1' sql
    

    set the variable n with the line number when first pattern matches, searches for the second pattern within 3 line neighborhood and inserts the lines after a match.