Solved

Regular expression to align comments

Posted on 2013-12-12
6
573 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
ID: 39714902
Have you looked at Perltidy?  It might work for the purpose.
0
 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 400 total points
ID: 39714921
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 29

Assisted Solution

by:serialband
serialband earned 100 total points
ID: 39714972
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:ambuli
ID: 39714982
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
ID: 39715156
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
ID: 39715201
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

829 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