Home > Net >  Perl Regex new line and no character
Perl Regex new line and no character

Time:06-08

I am trying extract multiple line from an Input file to Output file lines only containing head using regex in perl. Logic is to add lines as token in an array and then traverse array for head. Got stuck matching regex pattern new line and no character while adding lines as token in array.

my @arr = split("\n",$str);

foreach my $token (@arr) {
    print "Inside for\n";
    if($token =~ m[head])
    {
        print "Inside if";
        print $token;
    }
} 
**File Content**
**InputFile.txt**

- text1
- text2
- head

- text3
- text4
- non head

- text5
- text6
- head

**OutputFile.txt**
- text1
- text2
- head

- text5
- text6
- head

CodePudding user response:

The expected output can be achieved using the "sliding window" technique when reading the file line by line.

#!/usr/bin/perl
use warnings;
use strict;

my @buffer;
while (<>) {
    if (/- head$/) {
        print splice @buffer;
        print;
    } elsif (/^$/) {  # Same as ("\n" eq $_)
        @buffer = ("\n");
    } else {
        push @buffer, $_;
    }
}

CodePudding user response:

Can read input in paragraphs (-00 switch), if there is really always a blank like there, and print a paragraph if it ends with a desired pattern

perl -00 -wne'print if /\n\s*- head\s*\z/'  file

I use the \z assertion but $ is fine as well in the given example.


In a script this is done by setting the input record separator

use warnings;
use strict;

local $/ = "\n\n";

while (<>) { 
    print if /\n\s*- head\s*\z/;
}

We want local so to not change $/ for everything in the whole run (in a larger program).

  • Related