Sql File: Usage of Comments in SQL



SQL file
requires the elimination of all comments.

-- Sql comment line

What tool, such as GREP, can be used in Linux to accomplish this task?

Best Regards,

Solution 1:

The tool identified as


includes an option,


, that can invert the filter’s logic. This means that the opposite of the normal filtering behavior will occur.

grep -v pax people

Retrieve all the lines from the


file that do not include



An example is:

grep -v '^ *-- ' oldfile >newfile

The tool eliminates lines that have only empty space before a comment marker, but it does not alter lines such as:

select blah blah -- comment here.

To accomplish that task, you could utilize a tool such as



sed -e 's/ --.*$//' oldfile >newfile

The program modifies every line by deleting any characters starting from

" --"

until the end of the line.

When searching for the string

" --"

in actual SQL statements, such as the contrived one provided, it is important to exercise caution.

select ' -- ' | colm from blah blah blah

It is advisable to utilize an SQL parser instead of relying on a basic text modification tool, if you possess the former.

Here is a record of the


being executed.

pax$ echo '
...> this is a line with -- on it.
...> this is not
...> and -- this is again' | sed -e 's/ --.*$//'
this is a line with
this is not

For the



pax$ echo '
  -- this line starts with it.
this line does not
and -- this one is not at the start' | grep -v '^ *-- '
this line does not
and -- this one is not at the start

Solution 2:

The sed command can be utilized in the following manner:

sed -i '/--/d' <filename>


Solution 3:

Consider executing the shell command using the code



sed -e "s/(--.*)//" sql.filename

Frequently Asked Questions

Posted in Sql