Hi, I am trying to match the word (sample-text) and delete previous line if has a single word that might include (digit,_,-).
use strict;
use warnings;
my $filename = 'test1.txt';
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' $!";
my $line;
my $flag=1;
while($line=<$fh>)
{
if($flag == 0)
{
print $line;
next;
}
if($line=~/sample-text/)
{
$flag=0;
print $line;
}
}
Content of the test1.txt
Text_1
sample-text file Text_1
matching the 1st line
matching the last line
Text
text_1
text-2
sample-text file text-2
matching the 1st line
matching the last line
Expected output
sample-text file Text_1
matching the 1st line
matching the last line
text
text_1
sample-text file text-2
matching the 1st line
matching the last line
But it only removes the 1st line, and doesn't look for other lines. Please let me know.