Perl Regular Expressions + delete line if it starts with #

35,159

Solution 1

You don't delete lines with s///. (In a loop, you probably want next;)

In the snippet you posted, it would be:

while (my $line = <IN>) {
    if ($line =~ /^\s*#/) { next; }
    # will skip the rest of the code if a line matches
    ...
}

Shorter forms /^\s*#/ and next; and next if /^\s*#/; are possible.


perldoc perlre

/^\s*#/
  • ^ - "the beginning of the line"
  • \s - "a whitespace character"
  • * - "0 or more times"
  • # - just a #

Solution 2

Based off Aristotle Pagaltzis's answer you could do:

perl -ni.bak -e'print unless m/^\s*#/' deletelines.txt

Here, the -n switch makes perl put a loop around the code you provide which will read all the files you pass on the command line in sequence. The -i switch (for “in-place”) says to collect the output from your script and overwrite the original contents of each file with it. The .bak parameter to the -i option tells perl to keep a backup of the original file in a file named after the original file name with .bak appended. For all of these bits, see perldoc perlrun.

deletelines.txt (initially):

#a
b
     #a 
#   a
  c
         # a

becomes:

b
  c

Solution 3

Program (Cut & paste whole thing including DATA section, adjust shebang line, run)

#!/usr/bin/perl
use strict;
use warnings;
while(<DATA>) {
  next if /^\s*#/;  # skip comments
  print;            # process data
}
__DATA__
# comment
data
  # another comment
more data

Output

data
more data
Share:
35,159
Admin
Author by

Admin

Updated on February 02, 2020

Comments

  • Admin
    Admin almost 3 years

    How to delete lines if they begin with a "#" character using Perl regular expressions?

    For example (need to delete the following examples)

    line="#a"
    line="     #a"
    line="# a"
    line="    # a"
    

    ...

    the required syntax

       $line =~ s/......../..
    

    or skip loop if line begins with "#"

    from my code:

    open my $IN  ,'<', $file      or die "can't open '$file'  for reading: $!";
    while( defined( $line = <$IN> ) ){
    .
    .
    .
    
  • Admin
    Admin about 12 years
    but how this syntax delete the line?
  • Admin
    Admin about 12 years
    maybe like this $line =~ s/^\s*#//s; ?
  • user1686
    user1686 about 12 years
    @jennifer Updated answer. (s/^\s*#// would match the line, but it wouldn't delete it - it would replace the line with an empty one.)
  • Admin
    Admin about 12 years
    @grawity its not work I write also if (/^\s*#/) { print $line; } to see the remarked lines but its not print the remarked lines?
  • user1686
    user1686 about 12 years
    @jennifer: if you put it below the next;, then it's just skipped. Try if (...) { next; } else { print $line; } instead.
  • Admin
    Admin about 12 years
    I put if (/^\s*#/) { next; } else { print $line; } but now its print all lines include with "#" -:(
  • user1686
    user1686 about 12 years
    @jennifer Sorry, I forgot a part of the condition... It's supposed to be if ($line =~ /^\s*#/).
  • Felix Jassler
    Felix Jassler 9 months
    Won't quite work if the entire file is read, as ^ refers to the beginning of the file, not the beginning of a line. This worked for me: $text =~s/(^|\n)\s*#[^\n]*//g; (small caveat: there will be an obsolete newline if there's a comment on line number 1)