Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sed's N equivalent in Perl paragraph mode

0 views
Skip to first unread message

* Tong *

unread,
Jul 5, 2008, 5:52:30 PM7/5/08
to
Hi,

I want to remove this and next paragraph when criteria is met in this
paragraph. If expressed in sed, it'll be:

sed -paragraph_mode '/criteria/{N; d; }'

I tried the following in Perl, but neither works:

perl -n000e 'if (/criteria/) { next; next; }'

or,

perl -n000e '
if (/criteria/) { next; $skip_next=1; }
if ($skip_next) { next; $skip_next=0; }
'

Please help.

Thanks

Ben Morrow

unread,
Jul 5, 2008, 6:20:23 PM7/5/08
to

Quoth * Tong * <sun_to...@users.sourceforge.net>:

>
> I want to remove this and next paragraph when criteria is met in this
> paragraph. If expressed in sed, it'll be:
>
> sed -paragraph_mode '/criteria/{N; d; }'
>
> I tried the following in Perl, but neither works:
>
> perl -n000e 'if (/criteria/) { next; next; }'

This will output nothing. If you're using -n you need to print the lines
yourself.

The Perl equivalent of sed's 'N' (when using -n or -p) is '$_ .= <>'.
Since you are just throwing the line away, you don't need to append it
to $_. So that gives us

perl -n000e 'if (/criteria/) { <>; next; } print;'

I was going to suggest using 's2p', but it understands a rather limited
dialect of 'sed' and produces completely unreadable Perl, so, um, don't
:).

Ben

--
'Deserve [death]? I daresay he did. Many live that deserve death. And some die
that deserve life. Can you give it to them? Then do not be too eager to deal
out death in judgement. For even the very wise cannot see all ends.'
b...@morrow.me.uk

* Tong *

unread,
Jul 5, 2008, 10:29:55 PM7/5/08
to
On Sat, 05 Jul 2008 23:20:23 +0100, Ben Morrow wrote:

>> I want to remove this and next paragraph when criteria is met in this
>> paragraph. If expressed in sed, it'll be:
>>
>> sed -paragraph_mode '/criteria/{N; d; }'
>>

>> . . .


>
> The Perl equivalent of sed's 'N' (when using -n or -p) is '$_ .= <>'.
> Since you are just throwing the line away, you don't need to append it
> to $_. So that gives us
>
> perl -n000e 'if (/criteria/) { <>; next; } print;'

Thanks a lot. It works great. (yeah, I was so focusing on isolating the
question out of my code that I forgot about to include the print).

Thanks

0 new messages