[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Removing comments from perl source code file.

Posted on 2004-04-12
10
Medium Priority
?
167 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
[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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 500 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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 becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

649 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