views:

55

answers:

1

I've inherited a few dozen sql scripts that look like this:

select
    column_a,
    column_b,
    column_c
from
    my_table

To format them so they match the rest of our sql library, I'd like to change them to look like this:

select
    column_a
    ,column_b
    ,column_c
from
    my_table

where the commas start at the beginning of the line instead of at the end. I've taken a few passes at this in Perl, but haven't been able to get it to work just right.

Can any of you Perl gods provide some enlightenment here?

+5  A: 
perl -pi.bak -0777 -wle's/,[^\n\S]*\n([^\n\S]*)/\n$1,/g' file1.sql file2.sql ...

The character class is any non-newline whitespace. -0777 causes it to operate on whole files, not lines.

ysth
@ysth That's it! Awesome, thanks!
Data Monk