Link to home
Start Free TrialLog in
Avatar of ambuli
ambuliFlag for United States of America

asked on

Regular expression to align comments

I have a huge file with c structures with comments that are not aligned like below.

 // Structure 1
   {    1 , //Comment 0
       "ABC",   // Comment 1
       { 480 , 440 },        // Comment 2
       { 2000 , 4000 , 0 , 0 , 0 , 0 },    // Comment 3
       { 800 , 400 , 800 , 4000 , 0 , 0 },               // Comment 4

I want to make it look like:
                                                                             // Structure 1
   {    1 ,                                                                 //Comment 0
       "ABC",                                                            // Comment 1
       { 480 , 440 },                                                 // Comment 2
       { 2000 , 4000 , 0 , 0 , 0 , 0 },                        // Comment 3
       { 800 , 400 , 800 , 4000 , 0 , 0 },                 // Comment 4

since the file is very large I don't want to do it manually... Any regular expression that can do this?  thank you.
Avatar of ddrudik
ddrudik
Flag of United States of America image

Have you looked at Perltidy?  It might work for the purpose.
SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ambuli

ASKER

Thank you.  That works to some extent.  But, still tab introduces some problems.  How can I modify this so that I can say I want to move the comments to say column 50.
Try this:

perl -ne 'm{(.*?)\s+(//.*)}; $s = ((50 - length($1)) > 0 ? 50-length($1):0); printf("%s%${s}s\n", $1, $2)' filename

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial