I have a text file that I am processing inside perl script.
How do I remove 2 or more lines that contains only *
.
Input:
some text
*
some text
*
*
some text
*
*
*
I want to the text to look like this:
some text
*
some text
*
some text
*
I have a text file that I am processing inside perl script.
How do I remove 2 or more lines that contains only *
.
Input:
some text
*
some text
*
*
some text
*
*
*
I want to the text to look like this:
some text
*
some text
*
some text
*
You could read the whole file in.
perl -0777pe's/^\*\n\K(\*\n)+//mg'
(The above won't work as written if the line terminator is missing from the last line.)
Working line by line is also rather simple since there's no need to look ahead.
perl -ne'print if !$flag || !/^\*$/; $flag = /^\*$/;'
You can slurp, or anyway read your entire file into a string (today computer should have enough memory for that) and substitute any sequence of special lines with one only.
use English;
my $contents = do { local $/; <> };
chomp $contents;
$contents .= $RS;
$contents =~ s/^(\*$RS)+/\1/mg;
The 'm' modifier set the '^' anchor to recognise any line's begin, instead of the begin of the entire string.
Other possible solution with 'one liner' is
perl -0777 -pe "s/(\n\*)+/$1/g" regex_stars.txt
Input
some text
*
some text
*
*
some text
*
*
*
Output
some text
*
some text
*
some text
*