Solved

Removing comments from perl source code file.

Posted on 2004-04-12
10
155 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Disk Ram Peak 1 54
CSV file copy field 1 to field 2 2 33
what should i learn? javascript or typescript? 3 32
-OutVariable to Global 1 18
In this tutorial I will focus on how to use WhizBase as a tool for sending ICQ messages to ICQ. Here I will use a new technology in WhizBase, published in WhizBase 5.1 version. In this tutorial I will use 3 files, pager.wbsp for the processing, e…
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…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

679 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