tags:

views:

91

answers:

3

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.

+3  A: 

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 -- -- * `
mobrule
This works great but the only problem I see is that if the folders have a space (ex. SQL Scripts), then it fails. Not sure how to deal with that and one other problem (less critical) is that it is diving deep into tar files and trying to replace and is failing too. Thought? Thank you..
ThinkCode
Maybe an `xargs` solution? I'm not proficient with that tool, but something like `fgrep -l -- -- * | xargs perl -p -i -e '...'` might do the trick.
mobrule
A: 

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?

drewk
Yep, exactly... preferable a one liner..
ThinkCode
+3  A: 

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
Romuald Brunet
This is the right answer. Using Perl for this problem is overkill when there are excellent command-line utilities that will do the same for you with less fuss.
Ether
This makes a lot of sense too. Thank you.
ThinkCode
I prefer this answer for a one-liner, as it gets round the spaces-in-filenames problem by using find's --exec. Also, '--' at the beginning of the line is an important point.
FalseVinylShrub