Solved

Regular expression to align comments

Posted on 2013-12-12
6
575 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
Technology Partners: 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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