Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How do I strip lines prefixed by a "#"

( #38279=categorized question: print w/ replies, xml ) Need Help??
Contributed by Coplan on Oct 25, 2000 at 07:22 UTC
Q&A  > regular expressions


Description:

Let's assume that I have a text file with the following format:
# This is a commented line # blah blah blah :data :data
Well, I've been able to read the file into a scaler. Now i'd like to strip all the comment lines (prefixed by "#") out of the scaler before I move on and do anything else. For the sub routine, I am useing the following code:
sub get_info { my $infile = "template.dsc"; open (INFILE, "$infile") || die "Broken: $!"; my $content; while (<INFILE>) { $content .= $_; }; # Remove Comments? close (INFILE); return $content; };
Please help me get rid of that unwanted data (but I need to keep it in the text file for the idiots who use it).

Answer: How do I strip lines prefixed by a
contributed by runrig

Simplest is to:

while (<INFILE>) { next if /^#/; # or next if substr($_,0,1) eq '#'; $content .= $_; };
Answer: How do I strip lines prefixed by a
contributed by Fastolfe

Generally when I'm reading files that have the potential for comments or blank lines, a simple regular expression or two does the trick:

while (<DATA>) { s/\s#.*//; next unless /\S/; do_something($_); } __DATA__ # This is my file # with a few lines of comments data=here # another comment more=data # more comments and=more # end of file
Answer: How do I strip lines prefixed by a
contributed by Anonymous Monk

Use grep:

@code_lines = grep !/^#/, @all_lines;

The above code is taken directly from the Camel head book.

Answer: How do I strip lines prefixed by a
contributed by pppaulll

This bypasses lines where # is in column 1 and blank lines.

STLOAD: foreach $line (<INFILE>) { chomp $line; $line_header = substr($line, 0, 1); # Ignore comments (#) and blank lines # including 0-n spaces. if ($line_header eq '#' || $line =~ /^ *$/) { next STLOAD; } elsif ($line_header eq '@') { # Specifies: targ host, path, file $ts_cnt ++; if ($ts_cnt > 1) ...
Answer: How do I strip lines prefixed by a
contributed by runrig

Another option:

my $infile = "template.dsc"; open (INFILE, "$infile") || die "Broken: $!"; my $content; read INFILE, $content, -s INFILE; $content =~ s/^#.*//m; close INFILE; ...
Answer: How do I strip lines prefixed by a
contributed by tachyon

perl -pi.bak -e 's/^#.*\n//' <file>

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (7)
    As of 2014-09-02 02:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (18 votes), past polls