Solved

Regular expression to align comments

Posted on 2013-12-12
6
564 Views
Last Modified: 2013-12-12
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.
0
Comment
Question by:ambuli
6 Comments
 
LVL 27

Expert Comment

by:ddrudik
Comment Utility
Have you looked at Perltidy?  It might work for the purpose.
0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 400 total points
Comment Utility
Try something like


perl -pe 's{\s+//}{\t\t\t\t//}' filename

Open in new window


This is very specific to the above code.

When you see your output is good, you can use -i to make changes to the file
0
 
LVL 27

Assisted Solution

by:serialband
serialband earned 100 total points
Comment Utility
There's also several others more specifically designed for C, C++, Java style code.

http://universalindent.sourceforge.net/
http://astyle.sourceforge.net/
http://uncrustify.sourceforge.net/
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:ambuli
Comment Utility
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.
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
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

0
 
LVL 31

Accepted Solution

by:
farzanj earned 400 total points
Comment Utility
Actually, the above one doesn't consider non matched lines.

Try this instead

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

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now