Solved

Removing comments from perl source code file.

Posted on 2004-04-12
10
150 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

758 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

16 Experts available now in Live!

Get 1:1 Help Now