开发者

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

开发者 https://www.devze.com 2023-04-08 10:49 出处:网络
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.

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

精彩评论

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