开发者

how to remove a blank line after removing an unwanted line in perl?

For exampl开发者_高级运维e, I want to remove unwanted line with bbbb

aaaa
bbbb
cccc
dddd

I use the following perl regular expression to accomplish this.

$_ =~ s/bbbb//g;

The problem here is that a blank line is stays, for example

aaaa

cccc
dddd

I need to remove the unwanted text line and also the blank line.


You could simply include the newline in your regular expression:

$_ =~ s/bbbb\n//g;

This will result in:

aaaa
cccc
dddd


It seems to me that if you are reading this line by line you could just have your loop do this:

my @foo = (
    "aaaa\n",
    "bbbb\n",
    "cccc\n",
    "dddd\n" );

foreach my $line ( @foo ) {
    next if ( $line =~ /^bbbb$/ );

    # now do something with a valid line;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜