how to ignore multiline comment from a file while reading it

Hi friends , I want to ignore single and multiline comment( enclosed by " \* *\" ) of a file whle reading it. I am using the below code.

nawk '/\/\*/{f=1} /\*\//{f=0;next} !f' proc.txt | while read str
do
 ...
done

The problem is its working partially. that is its failing in one case.(shown below) i.e whenever there is a comment follwing a non comment in the same line, its deleting the whole line.

for example:

select col1 , col2 
          from                                       /* selecting columns from the table1 */
                    table1;
 
 

if we run the above script on the above code it will remove 2nd line totally.

OUPUT:

select col1 , col2 
        table1;

but the output should be :

select col1 , col2 
          from                                     
table1;

please help. thanks in advance

Try...

$ cat file1
:
select col1 , col2 from /* selecting columns from the table1 */ table1;
:
/* etc1
   etc2
*/
:
select col1 , col2
   from          /* selecting columns from the table1 */
     table1;
:

$ awk '{
          c = 0
          for (i = 1; i <= length; i++) {
              if (substr($0, i, 2)=="/*")
                   f = 1
              if (!f) {
                   printf substr($0, i, 1)
                   c++
              }
              if (substr($0, i, 2)=="*/") {
                   f = 0
                   i+=2
              }
          }
          if (!f && c)
              printf ORS
       }' file1 > file2

$ cat file2
:
select col1 , col2 from table1;
:
:
select col1 , col2
   from
     table1;
:

$
1 Like