0

Having a sql file (package), I want to remove all the comments (/*text*/) from the file with UNIX (sed). This is what I have tried:

sed 's/[:blank:]*-~.*[\n]*.*~-$//' file.txt

It seems that the line feed character does not work that way. Any suggestions? It should remove from line 3 to 8. And line 15.

SQL File:

CREATE OR REPLACE PACKAGE BODY MYSCHEMA.MYPACKAGE
AS
/***************************************************************************
    Author         : myname
    Date 10/2019
    Overview       : This is the main control procedure
   Major Modifications (initial, when, ver, irfs, description):
***************************************************************************/
PROCEDURE MYPROCEDURE (p_error        IN OUT   NUMBER,
                                        p_return_code     IN OUT   NUMBER,
                                        p_error_message   IN OUT   VARCHAR2)
AS
v_infomsgid             NUMBER;
BEGIN
/*This is my comment about this process*/
   MY_SQL_CODE_HERE;
END;
END MYPACKAGE;

The desired output is this:

CREATE OR REPLACE PACKAGE BODY MYSCHEMA.MYPACKAGE
AS
PROCEDURE MYPROCEDURE (p_error        IN OUT   NUMBER,
                                        p_return_code     IN OUT   NUMBER,
                                        p_error_message   IN OUT   VARCHAR2)
AS
v_infomsgid             NUMBER;
BEGIN
   MY_SQL_CODE_HERE;
END;
END MYPACKAGE;

Thanks!

Walter A
  • 19,067
  • 2
  • 23
  • 43
eJossue
  • 11
  • 1
  • 1
    Please add your desired output (no description) for that sample input to your question (no comment). – Cyrus Mar 13 '20 at 20:07
  • Doing it with `sed` is almost impossible, because regex is greedy and there are no lookarounds. Lines like `/* comment */ code /* comment */` are going to be hard. – KamilCuk Mar 13 '20 at 21:10
  • Does this answer your question? [Remove comments from C/C++ code](https://stackoverflow.com/questions/2394017/remove-comments-from-c-c-code) – KamilCuk Mar 13 '20 at 21:10
  • If you want to remove `/text/` then those from your example is a problem: `10/2019`. – Cyrus Mar 13 '20 at 21:19
  • @Cyrus I edited the question, OP did not use backquotes around `/*text*/`. – Walter A Mar 14 '20 at 17:01

1 Answers1

0

You could look at the link @KamilCuk gave in a comment for dedicated tools or use GNU sed:

sed -z 's#\*/#\r#g;s#/\*[^\r]*\r\n*##g' file.txt

Explanation:
First replace */ with a unique character (I will use \r, because you should already have removed the Windows line endings). Next you can remove the comment.
Using option -z is for matching comments that last more than 1 line, and removing the newline after the end of a comment.

Walter A
  • 19,067
  • 2
  • 23
  • 43