开发者

Using Perl to move commas from end of line to begining of line

开发者 https://www.devze.com 2023-01-11 11:35 出处:网络
I\'ve inherited a few dozen sql scripts that look like this: select column_a, 开发者_StackOverflow社区 column_b,

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

select
    column_a,
   开发者_StackOverflow社区 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?


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.

0

精彩评论

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