Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

How do I strip lines prefixed by a "#"

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


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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    [Spenser]: It uses opkg to install software
    [choroba]: 5.10.1 would be much better
    [shmem]: there are so many ways thinkable to fix that situation, some feasible and some not, that I'm unable to give good advice.
    [Spenser]: I installed 5.22 with opkg. I can use it with cpan. I'm actually trying to install perl-DBD-mysql, but couldn't with it. So I went back to the factory version, 5.10.
    [Spenser]: So, nevermind about CPAN. I give up on the downgraded angle. I'm back to fighting to install DBD.
    [shmem]: please open a SoPW. At what point is the perl-DBD-mysql installation failing?
    [shmem]: do you have the mysqlclient libs on that device, for instance?
    [shmem]: and btw, your original questian is an XY Problem classic ;-)

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (8)
    As of 2018-03-17 12:42 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (224 votes). Check out past polls.