Solved

Regular expression to align comments

Posted on 2013-12-12
6
565 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 28

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.

914 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

23 Experts available now in Live!

Get 1:1 Help Now