Solved

Removing comments from perl source code file.

Posted on 2004-04-12
10
153 Views
Last Modified: 2013-12-25
Hello everyone,

    How to remove all of the # starting lines in a perl source code file? It's way too big to do it by hand. Thanks for any help.
0
Comment
Question by:intoxicated
  • 3
  • 2
  • 2
  • +3
10 Comments
 

Expert Comment

by:mannie
ID: 10805890
Something like this perhaps

#!/bin/perl

open FILE,"filetostrip.pl" or die("Failed to open");
@lines=<FILE>;
close FILE;

open FILEAGAIN, ">filetostrip.pl" or die("Failed to reopen");
foreach (@lines) {
 print $_ unless (/^[#](.*)$/);
}
close FILEAGAIN;

/Linus
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 10807600
perl -pi -e 's/^#.*$//' script.pl
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 10807643
Oops, that will also remove the shebang line; this one won't

perl -pi -e 's/^#.*$// unless $.==1' script.pl
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.

 
LVL 84

Expert Comment

by:ozo
ID: 10808200
If you have
#
inside of quoted strings it will remove them too
0
 
LVL 2

Expert Comment

by:Fataqui
ID: 10815163
Hi


I did not add put a line empty line clean up, but you can add on before saving the new code...

$file = '';

open FILE, "<" . $file or die ( "Can not open file" );
while ( <FILE> )
{
      tr/\r//d;

      if ( !m/^#!/ )
      {

            s/(^|\n|\s)*#[^\n]*//g;

      }

      $out .= $add . $_;

}

close FILE;

open NEW, ">" . $file;
print NEW $out;
close NEW;




Fataqui
0
 
LVL 2

Expert Comment

by:Fataqui
ID: 10815229
Hi

This line....

     $out .= $add . $_;

should be....

     $out .= $_;

I put the $add there to insert new lines, but seeing I did not know if you wanted to strip \r?\n I took that part out and forgot to remove $add, before posting, sorry!


Fataqui
0
 
LVL 1

Expert Comment

by:Glauron
ID: 10830022
all very good and all..... but WHY would you want to remove the comments!?!?  Especially if the file is so big!!
Hope you can find your way around it again  =)

..... unless ur just formatting for some other use...
.. oh... nm
0
 

Author Comment

by:intoxicated
ID: 10830833
Thenks alot lads. FishMonger, you're a star ;). Is there a way for your single-liner to remove the whole comment line (now it leaves an empty line, making wide areas of empty space)?? Why do I need it? To check how much of the file is actual code.
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 125 total points
ID: 10833724
Simple, just add \n to the end of the seach pattern.

perl -pi -e 's/^#.*\n$// unless $.==1' script.pl
0
 

Author Comment

by:intoxicated
ID: 10833838
That worked like charm. Thanks again.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

815 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

12 Experts available now in Live!

Get 1:1 Help Now