http://www.perlmonks.org?node_id=38279

Coplan has asked for the wisdom of the Perl Monks concerning the following question:

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).

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I strip lines prefixed by a
by runrig (Abbot) on Oct 25, 2000 at 07:32 UTC
    Simplest is to:
    while (<INFILE>) { next if /^#/; # or next if substr($_,0,1) eq '#'; $content .= $_; };
      Ok....that works great!

      But I am (unfortunately) a beginner Perl Hacker. So could you (or someone) please explain what exactly you did?

      I think it's the ^ that confuses me a bit.

Re: How do I strip lines prefixed by a
by Fastolfe (Vicar) on Oct 25, 2000 at 20:19 UTC
    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
      That first regexp should have read: s/\s*#.*//;
Re: How do I strip lines prefixed by a
by Anonymous Monk on Apr 03, 2001 at 23:15 UTC
    Use grep:

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

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

Re: How do I strip lines prefixed by a
by pppaulll (Initiate) on Nov 29, 2001 at 21:23 UTC
    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) ...
Re: How do I strip lines prefixed by a
by runrig (Abbot) on Oct 25, 2000 at 07:55 UTC
    Another option:
    my $infile = "template.dsc"; open (INFILE, "$infile") || die "Broken: $!"; my $content; read INFILE, $content, -s INFILE; $content =~ s/^#.*//m; close INFILE; ...
      correction: make that s/...//gm; oops! forgot the 'g' :-)
Re: How do I strip lines prefixed by a
by tachyon (Chancellor) on Feb 27, 2003 at 13:21 UTC
Re: How do I strip lines prefixed by a
by mslattery (Initiate) on Apr 19, 2002 at 18:55 UTC
    KISS

    next if /^\#/ ;

    thanks,
    mslattery

    Originally posted as a Categorized Answer.

Re: How do I strip lines prefixed by a
by Anonymous Monk on Feb 27, 2003 at 11:45 UTC
    you can use the bash grep running externally grep -v '^#' file
      Administrator@C ~
      $ cat t
      one
      #two
      #three
      four
      #five
      six
      
      Administrator@C ~
      $ perl -ne 'print if not m/^#.*/' <t
      one
      four
      six
      
      (or in K:
      x@&~"#"=*:'x
      )