ambuli
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.
// 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.
Have you looked at Perltidy? It might work for the purpose.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.