Solved

Regular expression to align comments

Posted on 2013-12-12
6
582 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Fine Tune your automatic Updates for Ubuntu / Debian
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Six Sigma Control Plans

729 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