How to remove characters between <and> in Perl?

I need to write a Perl script to read in a file and delete something inside <>, even if they are on different lines. That is, if the input:

Hello, world. I <enjoy eating
bagels. They are quite tasty.
I prefer when I ate a bagel to
when I >ate a sandwich. <I also
like >bananas.

I want the output to be:

Hello, world. I ate a sandwich. bananas.

I know how to do this if the text is in 1 line with a regular expression. But I do not know how to do this with multiple lines. Ultimately, I need to be able to conditionally delete parts of the template so that I can create parameterized files for configuration files. I thought perl would be a good language, but I still feel it.

Change . More than one instance is also required <>

+5
source share
4
local $/;
my $text = <>;
s/<.*?>//gs;
print $text;
+4

Perl Text:: Balanced, . , . , , , , , , .

+6

Perl:

#! /usr/bin/perl   
use strict;

my $text = <>;
$text =~ s/<[^>]*>//g;
print $text;

, < > () . G ( ).

EDIT: Hynek

+6

perl -0777 -pe 's/<.*?>//gs'

local $/;
my $text = <>;
s/<.*?>//gs;
print $text;

It depends on how the large text you want to convert is the more efficient consumption of one line per line

perl -pe 'if ($a) {(s/.*?>// and do {s/<.*?>//g; $a = s/<.*//s;1}) or $_=q{}} else {s/<.*?>//g; $a = s/<.*//s}'

similar to program

my $a;
while (<>) {
    if ($a) {
        if (s/.*?>//) {
            s/<.*?>//g;
            $a = s/<.*//s;
        }
        else { $_ = q{} }
    }
    else {
        s/<.*?>//g;
        $a = s/<.*//s;
    }
    print;
}
+1
source

All Articles