开发者

How can I convert SQL comments with -- to # using Perl?

UPDATE:

This is what works!

fgrep -ircl --include=*.sql -- -- *

I have various SQL files with '--' 开发者_如何转开发comments and we migrated to the latest version of MySQL and it hates these comments. I want to replace -- with #.

I am looking for a recursive, inplace replace one-liner.

This is what I have:

perl -p -i -e 's/--/# /g'` ``fgrep -- -- *  

A sample .sql file:

use myDB;

--did you get an error

I get the following error:

Unrecognized switch: --did (-h will show valid options).

p.s : fgrep skipping 2 dashes was just discussed here if you are interested.

Any help is appreciated.


The command-line arguments after the -e 's/.../.../' argument should be filenames. Use fgrep -l to return names of files that contain a pattern:

perl -p -i -e 's/--/# /g' `fgrep -l -- -- * `


I'd use a combination of find and inplace sed

find . -name '*.sql' -exec sed -i -e "s/^--/#/" '{}' \;

Note that it will only replace lines beginning with --
The regex will become vastly more complex if you wan't to replace this for example:

INSERT INTO stuff VALUES (...) -- values used for xyz

because the -- might as well be in your data (I guess you don't want to replace those)

INSERT INTO stuff VALUES (42, "<!-- sboing -->") -- values used for xyz


The equivalent of that in script form is:

#!/usr/bin/perl -i
use warnings;
use strict;

while(<>) {
    s/--/# /g;
    print;

}

If I have several files with comments of the form of --comment and feed any number of names to this script, they are changed in place to # comment You could use find, ls, grep, etc to find the files...

There is nothing per se wrong with using a 1 liner.

Is that what you are looking for?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜